I’m a iOS developer of the iWorld.
I’ve developed several iPhone/iPad apps before.
I couldn’t get correct time information on iPhone several times. I think that is due to localization.
For example, I have code to get the time-truncated today. (ex : “05-04-2012 00:00:00”)
But that code always return 8 hours ago to that I’ve expected. (ex : “05-03-2012 16:00:00)
The code that I’ve worked are as following.
NSDate *today = [NSDate date];
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calender components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
today = [calender dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy hh:mm:ss"];
NSString *todayString = [formatter stringFromDate:today];
NSLog(@"Today is %@", todayString);
I’d like to hear from you to resolve this problem.
Thanks for your time.
I’ve found that code that calculates correct midnight time.
Now I find that following code logs “Today is 05-07-2012 00:00:00” rather than “05-06-2012 16:00:00”
Thanks for SO.