xcode 如何将unix时间戳转换为人类可读的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14121403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:26:55  来源:igfitidea点击:

How to convert unix timestamp to human readable time?

iphoneobjective-ciosxcodensdate

提问by zzzzz

I get a unix timestamp from the database and I am trying to create a human readable date from it. I am using this way

我从数据库中获得了一个 unix 时间戳,我正在尝试从中创建一个人类可读的日期。我正在使用这种方式

long t1=[time longLongValue];

NSDate* date=[NSDate dateWithTimeIntervalSince1970:t1];

where time is the timestamp. When I print date I get

其中 time 是时间戳。当我打印日期时,我得到

1956-02-18 19:04:01 +0000 

instead of

代替

2013-01-02 12:31:03 +0000

The timestamp was 1356765933449

时间戳是 1356765933449

回答by Gabriele Petronella

It is a matter of integer overflow, as Boris correctly pointed out in his answer.

正如鲍里斯在他的回答中正确指出的那样,这是一个整数溢出问题。

I don't know what your timeobject is, but instead of a signed long intuse a NSTimeInterval.

我不知道你的time对象是什么,但不是signed long int使用 a NSTimeInterval

On iOS NSTimeIntervalis currently defined as

在 iOSNSTimeInterval上当前定义为

typedef double NSTimeInterval;

but you shouldn't care too much about that. Sticking with type synonyms will protect you in case Apple decides to change the underlying definition to something else.

但你不应该太在意这个。如果 Apple 决定将底层定义更改为其他内容,则坚持使用类型同义词可以保护您。

That said you should change your code to something like

那就是说您应该将代码更改为类似

NSTimeInterval epoch = [time doubleValue];
NSDate * date = [NSDate dateWithTimeIntervalSince1970:epoch];

Concerning the code maintainability issue I described before, here you are explicitly using a doubleValue(you don't have many options), but the good thing is that if Apple changes the NSTimeIntervaldefinition to something not compatible with a doubleassignment, the compiler will let you know.

关于我之前描述的代码可维护性问题,这里您明确使用了 a doubleValue(您没有很多选择),但好消息是,如果 Apple 将NSTimeInterval定义更改为与double赋值不兼容的内容,编译器会通知您.

回答by Talha

Try this

尝试这个

- (NSString *) getDateFromUnixFormat:(NSString *)unixFormat
{

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[unixFormat intValue]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MMM dd, yyyy-h:mm"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    //NSDate *date = [dateFormatter dateFromString:publicationDate];
    NSString *dte=[dateFormatter stringFromDate:date];

    [dateFormatter release];
    return dte;

}

回答by Boris Prohaska

The Unix timestamp has only 32 Bits available.

Unix 时间戳只有 32 位可用。

Because they use a signed int, they count the seconds from 1.1.1970. A 32 Bit signed int can only hold values up to 2147483647, where as you want it to be 1356765933449. That causes an overflow, and that causes your date to be invalid.

因为他们使用有符号整数,所以他们从 1.1.1970 开始计算秒数。一个 32 位有符号 int 最多只能保存 2147483647 的值,如您所愿1356765933449。这会导致溢出,从而导致您的日期无效。

This is also known as the Year 2038 Problem, because 2147483647 (max value) will be hit on 03:14:07 UTC on Tuesday, 19 January 2038.

这也称为Year 2038 Problem,因为 2147483647(最大值)将在 2038 年 1 月 19 日星期二 UTC 时间 03:14:07 命中。