objective c - How can one display Gregorian calendar year for an NSDate when calendar in iOS settings is Japanese or Buddhist -


i trying display 4 digit gregorian year of nsdate while calendar set japanese.

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]); 

log:

date joined: 2012-05-29 22:13:54 +0000 year joined: 4000 

what doing wrong?

try this. works.

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]; 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -