开发者

Dateformatter iPhone issue

NSLog(@"%@", self.departDate);

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSLog(@"%@", [dateFormat stringFromDate:self.departDate]);
[dateFormat release];

So I have this code, which returns this in the console:

2011-06-09 00:00:00 +0000
06/08/2011

Any ideas why this is happening? As you can see, my self开发者_Python百科.departDate object returns 2011-06-09 as it's date. When I try to use date formatter to convert, I lose a day WTF?


The first log corresponds to a GMT date. However when you create an NSDateFormatter instance, it will have a default timezone set to the user's timezone which I am guessing is probably somewhere west of Greenwich. If you want the time to remain in GMT, you will have to add this line,

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜