0

简单的问题。我有一些肮脏的解决方案,但无法找到一个干净、强大的解决方案。问题是我有 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

谢谢

阿尔达

4

1 回答 1

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];
于 2013-07-25T06:53:56.367 回答