开发者

Convert time from milliseconds to human readable date

开发者 https://www.devze.com 2023-04-11 02:00 出处:网络
I have this time in milliseconds that i am receiving in an XML feed, and i need to display 开发者_JS百科this time in any appropriate form, that anyone can understand.

I have this time in milliseconds that i am receiving in an XML feed, and i need to display 开发者_JS百科this time in any appropriate form, that anyone can understand.

can anyone guide me on how to achieve such behavior?

thank you.

Note: the millis time that i am getting is in UNIX time.


Use NSDate's dateWithTimeIntervalSince1970: method by dividing your timestamp by 1000.0.

NSDate* date = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];

You can refer to this question for more details on formatting.


Create an NSDate object with your interval and then use an NSDateFormatter object to turn that into a display value, like this:

- (void)displayDate:(double)unixMilliseconds {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:unixMilliseconds / 1000.];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    NSLog("The date is %@", [dateFormatter stringFromDate:date]);
    [dateFormatter release];
}

Obviously you should allocate the formatter once and keep it around if you do this a lot.

0

精彩评论

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

关注公众号