开发者

NSDateFormatter -- Not getting the correct formatter string

I am getting a wrong formatted date string returned by NSDateFormatter. Here is my code:

NSDateFormatter *aDateFormatter开发者_开发知识库 = [[NSDateFormatter alloc] init];
[aDateFormatter setDateFormat:@"dd-MM-yy HH:mm:ss"];
NSString *aFormattedElapsedTime = [aDateFormatter stringFromDate:refDate];

Here, refDate (NSDate object) = 1970-01-01 00:36:22 +0000

Output I am getting is "16h:36m:22s" instead of "00h:36m:22s".

Am I doing something wrong here?


NSDateFormatter will default to current time zone on the system unless you set it with setTimeZone.

[aDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Take a look at NSDateFormatter to NSString?


Try setting the timezone. (See my comment above)

NSDateFormatter *aDateFormatter = [[NSDateFormatter alloc] init];
[aDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]
[aDateFormatter setDateFormat:@"dd-MM-yy HH:mm:ss"];
NSString *aFormattedElapsedTime = [aDateFormatter stringFromDate:refDate];


Aside from living in Pacific Time, not really. Add your timezone offset in there and you'll understand why you're getting that value: NSDateFormatter is formatting the date as interpreted in local time, not UTC.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜