将时间从毫秒转换为人类可读日期

时间:2022-09-27 23:15:40

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

我有这个时间,我在XML Feed中收到的时间是毫秒,我需要以任何适当的形式显示这个时间,任何人都可以理解。

can anyone guide me on how to achieve such behavior?

任何人都可以指导我如何实现这样的行为?

thank you.

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

注意:我得到的毫秒时间是在UNIX时间。

2 个解决方案

#1


6  

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

使用NSDate的dateWithTimeIntervalSince1970:方法,将时间戳除以1000.0。

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

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

您可以参考此问题以获取有关格式化的更多详细信息。

#2


1  

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

使用您的间隔创建一个NSDate对象,然后使用NSDateFormatter对象将其转换为显示值,如下所示:

- (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.

显然你应该分配一次格式化程序并保留它,如果你做了很多。

#1


6  

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

使用NSDate的dateWithTimeIntervalSince1970:方法,将时间戳除以1000.0。

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

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

您可以参考此问题以获取有关格式化的更多详细信息。

#2


1  

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

使用您的间隔创建一个NSDate对象,然后使用NSDateFormatter对象将其转换为显示值,如下所示:

- (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.

显然你应该分配一次格式化程序并保留它,如果你做了很多。