0

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.

Brian
  • 15,599
  • 4
  • 46
  • 63
Pei
  • 11,452
  • 5
  • 41
  • 45
  • So what time do you expect; localtime or UTC? – trojanfoe May 04 '12 at 14:59
  • I'd like to know to know the correct midnight time. Because I have to compare date against database date. For example, I have to find the record for today those are between 05-04-2012 00:00:00 and 05-05-2012 00:00:00 for just today. – Pei May 04 '12 at 15:02
  • So do you want localtime or UTC? – trojanfoe May 04 '12 at 15:07
  • I'd like to base on UTC. – Pei May 04 '12 at 15:08
  • Sorry, I'd like to base on local time. – Pei May 04 '12 at 15:26
  • This might do it then: http://stackoverflow.com/questions/1268509/convert-utc-nsdate-to-local-timezone-objective-c – trojanfoe May 04 '12 at 15:28
  • Thanks, trojanfoe. And I've found some useful things on SO just now. I've found "today = [today dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];" – Pei May 04 '12 at 15:51

2 Answers2

0

Set timezone to UTC

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
Kirby Todd
  • 11,254
  • 3
  • 32
  • 60
0

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"

NSDate *today = [NSDate date];
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calender components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
today = [calender dateFromComponents:components];

today = [today dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM-dd-yyyy hh:mm:ss"];
NSString *todayString = [formatter stringFromDate:today];
NSLog(@"Today is %@", todayString);

Thanks for SO.

Pei
  • 11,452
  • 5
  • 41
  • 45