2013-07-25 1 views
0

간단한 질문입니다. 나는 더러운 해결책이 있지만 깨끗하고 견고한 해결책을 찾지 못했습니다. 문제는 내가 dayofyear (SQL Server에서), NSDate로 변환하고 싶습니다. 구체적으로 :DAYOFYEAR

int dayofyear = 205 //I have this 

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

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

감사

아르

답변

4

"올해의 시작을"가져 오기 시작을 얻기를 위해

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

1 :

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 일 추가 달력 단위의 날짜. 나는이 방법에 대해 전혀 몰랐다. 고마워! :-) –

+0

@LukasKukacka : 예, 매우 편리합니다. 'interval' 매개 변수를 사용하면 마침표도 얻을 수 있습니다. 현재 날짜의 시작/끝 날짜 –

+0

아직 구현되지 않았지만 완벽 해 보입니다. 고맙습니다! – ardavar