开发者

DateFormat from date get wrong date

NSLog(@"Time From DB %@", mytime.beginTime);     
NSD开发者_运维技巧ateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"hh:mm a"];
UILabel *timeLabel = (UILabel *)[cell viewWithTag:1];
timeLabel.text = [timeFormatter stringFromDate:mytime.beginTime];
NSLog(@"Time after formatting %@", timeLabel.text);
[timeFormatter release];

Console

2011-07-05 11:16:09.331 My project[1907:207] Time From DB 2011-05-10 13:54:09 +0000
2011-07-05 11:16:09.331 My project[1907:207] Time after formatting 04:54 PM

Why are the times different? How to fix that?


It is using the user's time zone (default). If you want it in GMT, you will have to set the time zone of the date formatter.

[timeFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

BTW Times aren't different. Time is the same but is being represented in different time zones.


The date 2011-05-10 13:54:09 +0000 has the value in the GMT time zone, as you can notice this from +0000 in the date. So, while converting, the date formatter uses the phone's time zone, that is your location's time zone, which is -03:00, it seems. So in order to convert a date in GMT time zone to a GMT time zone date, you need to explicitly set the time zone of the date formatter to GMT, if GMT is not the phone's default time zone, as specified in @Deepak's answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜