2010-06-17 35 views
1

我創建這樣的年月日時:怪異的行爲創造一月一個NSDate第1和第2

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"YYYY"]; 
NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; 
NSLog(@"NSInteger YEAR : %d\n", year); 

//minutes, hours, day and month are initialized elsewhere... 

NSDateComponents *comps = [[NSDateComponents alloc] init];  
[comps setYear:year]; 
[comps setMonth:month]; 
[comps setDay:day]; 
[comps setHour:hours]; 
[comps setMinute:minutes]; 
NSDate *sampleDate = [gregorian dateFromComponents:comps]; 

NSLog(@"sampleDate YEAR : %d\n", [[dateFormatter stringFromDate:sampleDate] intValue]); 

我的問題是,當日期是1月1日或1月2日,這一年是不正確..在這裏它應該是2010年(因爲我今年獲得),但實際上它是2009年......我不明白爲什麼!而這僅僅是發生了這2天... ...

你有任何想法,爲什麼是這樣呢? 預先感謝您。

回答

5

Unicode standard。 「yyyy」(小寫字母)表示年份,但「YYYY」(大寫字母)表示日期的一年(也就是說,如果日期在去年的第52周內,則YYYY將爲去年)。

+0

是的,它的工作原理..謝謝。感謝您的鏈接! – leochab 2010-06-18 07:06:10

相關問題