2013-10-07 32 views
0

我試圖在日曆設置爲日語時顯示NSDate的4位數公曆年。當iOS設置中的日曆是日語或佛教徒時,如何顯示公曆日曆年的NSDate

NSLog(@"Date joined: %@", self.dateJoined); 
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
formatter.calendar = calendar; 
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"EST"]; 
formatter.dateFormat = @"yyyy"; 
NSLog(@"Year joined: %@", [formatter stringFromDate:self.dateJoined]); 

登錄:

Date joined: 2012-05-29 22:13:54 +0000 
Year joined: 4000 

我在做什麼錯?

+0

您是否試過「en_US_POSIX」而不是「en_US」? –

+0

@MartinR我只是試了一下,並得出相同的結果。 –

回答

0

試試這個。有用。

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

NSDateComponents *dateComps = [cal components:(NSYearCalendarUnit) fromDate:self. dateJoined]; 

NSLog(@"date = %d",[dateComps year]); 

// IGNORE IF USING ARC 
[cal release];