I'm making an app that supports conversion of dates from the Buddhist calendar to Gregorian (Note: The "General > International > Calendar" settings of the device I'm testing on is "Buddhist".). However, I can't figure out why the NSDateFormatter doesn't parse my dates properly. Here's my code:
NSDate *now = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.calendar = gregorianCalendar;
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
NSString *formattedDate = [formatter stringFromDate:now];
NSDate *boomDate = [formatter dateFromString:formattedDate];
NSLog(@"now: %@, formattedDate (as string) > %@, boomDate (as date) > %@", now, formattedDate, boomDate);
Xcode's log says:
now: 2556-05-23 07:11:03 +0000, formattedDate (as string) > 2013-05-23 15:11:03 +0800, boomDate (as date) > 2556-05-23 07:11:03 +0000
When I'm converting the formattedDate
(which is an NSString) to an NSDate, why does my NSDateFormatter parse it according to the Buddhist calendar format even if I set it's properties properly (especially the formatter.calendar
). I need my formattedDate
converted as an NSDate with the Gregorian calendar format. Based on its logic, I'm expecting the NSDateFormatter to give me a date with the Gregorian format but it doesn't.
Basically, I need an NSDate following the Gregorian format from a Buddhist NSDate.
Any ideas?