1

I want to change UTC time to local time by NSDateFormatter.

But it doesn't work what I expect.

-(NSDateFormatter *)df
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
    dateFormatter.timeZone = [NSTimeZone defaultTimeZone];

    return dateFormatter;
}


...

NSDate *now = [NSDate date];

NSString *dateStr = [[self df] stringFromDate:now];
// dateStr is correct.
//dateStr is 2013-12-26 20:32:26 +0900

NSLog(@"dateStr is %@",dateStr);

NSDate *date = [[self df] dateFromString:dateStr];
// date is wrong.
//date is 2013-12-26 11:32:26 +0000

NSLog(@"date is %@",date);

Why date is not equal to dateStr.

I want date return 2013-12-26 10:32:26 +0900

Do you have any idea? Thanks in advance.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
nobinobiru
  • 792
  • 12
  • 28
  • Or [Why NSDate is reporting the wrong date?](http://stackoverflow.com/questions/6741519/why-nsdate-is-reporting-the-wrong-date) (and probably some more ...) – Martin R Dec 26 '13 at 11:43

2 Answers2

3

The NSDate *date that you get is correct - 2013-12-26 11:32:26 +0000 is the UTC representation of 2013-12-26 20:32:26 +0900. NSDate objects do not embed the time zone - they represent an abstract point in time, not a point in time at any specific time zone.

NSLog always logs time in in UTC, because it calls the description method of NSDate.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

You need to change the time zone of your final Date to get the desired result.

Akshat Singhal
  • 1,801
  • 19
  • 20