每天到NSDate的目标c

时间:2013-07-25 06:39:14

标签: objective-c nsdate dayofweek

简单的问题。我有一些肮脏的解决方案,但无法找到一个干净,健壮的解决方案。问题是我有dayofyear(来自SQL Server),我想将其转换为NSDate。详细说明:

int dayofyear = 205  //I have this 

NSDate* date =  ??something by adding dayofyear to 1/1/currentyear(2013)??

//must have :  date = 07/25/2013

由于

阿尔达

1 个答案:

答案 0 :(得分:4)

获取“当年的开始”:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfYear;
[cal rangeOfUnit:NSYearCalendarUnit startDate:&startOfYear interval:NULL
         forDate:now];
// startOfYear represents now the start of the current year.

添加205天:

int dayofyear = 205;
NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setDay:dayofyear];
NSDate *newDate = [cal dateByAddingComponents:comp toDate:startOfYear options:0];
相关问题