Objective-C:帮助我将时间戳转换为格式化的日期/时间

发布于 2024-12-06 03:25:37 字数 659 浏览 1 评论 0原文

我真的很难将此时间戳转换为格式良好的日期字符串。

这是时间戳:“1316625985”

这是我所做的函数:

-(NSString*)timestamp2date:(NSString*)timestamp{
    NSString * timeStampString =timestamp;
    //[timeStampString stringByAppendingString:@"000"];   //convert to ms
    NSTimeInterval _interval=[timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"dd/MM/yy"];
    return [_formatter stringFromDate:date];
}

问题是,它一直返回 1972 年的日期! (31/7/72) 这是错误的,因为它是 2011 年 9 月的日期...

任何人都可以提出任何解决方案吗?

预先非常感谢,

I'm really struggling to convert this timestamp to a nice formatted date string.

Here's the timestamp: "1316625985"

And here's the function I've made:

-(NSString*)timestamp2date:(NSString*)timestamp{
    NSString * timeStampString =timestamp;
    //[timeStampString stringByAppendingString:@"000"];   //convert to ms
    NSTimeInterval _interval=[timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"dd/MM/yy"];
    return [_formatter stringFromDate:date];
}

Trouble is, it keeps returning dates in 1972! (31/7/72) This is wrong, since it's a September 2011 date...

Can anyone suggest any solution?

Many thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

‖放下 2024-12-13 03:25:37

纪元不是自 1970 年 1 月 1 日以来的吗?你确定你没有留下毫秒行吗?

当我运行你的逻辑时(没有你的行附加 000 毫秒),它起作用了。这就是我所做的:

    NSString * timeStampString = @"1316641549";
    NSTimeInterval _interval=[timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSLog(@"%@", date);

它输出:

2011-09-21 17:46:14.384 Craplet[13218:707] 2011-09-21 21:45:49 +0000

Isn't epoch seconds since 1/1/1970? are you sure that you didn't leave the millisecond line in?

When I ran you're logic (without your line append 000 for ms), it worked. Here's what I did:

    NSString * timeStampString = @"1316641549";
    NSTimeInterval _interval=[timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSLog(@"%@", date);

It outputted:

2011-09-21 17:46:14.384 Craplet[13218:707] 2011-09-21 21:45:49 +0000

吹梦到西洲 2024-12-13 03:25:37

只需将 [timeStampString doubleValue] 除以一千即可:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue]/1000.0];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:@"dd/MM/yy"];
return [_formatter stringFromDate:date];

或者尝试 [timeStampString longLongValue]/1000.0 祝

你好运!

Just divide your [timeStampString doubleValue] by a thousand like:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue]/1000.0];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:@"dd/MM/yy"];
return [_formatter stringFromDate:date];

or try [timeStampString longLongValue]/1000.0 instead

Good Luck!

热鲨 2024-12-13 03:25:37

您是否检查过字符串 (timestampString) 和双精度值 (_interval) 是否相同,并且 doubleValue 确实获取了所有你的字符串的字符考虑在内?

也许将字符串解释为双重作物的值?


您是否还确定您正在解释的时间戳确实是 UNIX 时间戳,这意味着它计算自 01/01/1970 以来经过的秒数(而不是自 01/01/1970 以来的天数?...或者不是秒而是自另一个日期以来的秒数? )

(也许给出您尝试解释的时间戳值的示例)

Did you check that the string (timestampString) and the double value (_interval) are the same, and that the doubleValue does really takes all the characters of your string into account?

Maybe the interpretation of the string into double crops the value?


Are you also sure that the timestamp you are interpretting is really a UNIX timestamp, meaning it counts the number of seconds elapsed since 01/01/1970 (and not days since 01/01/1970?… or not seconds but since another date?)

(Maybe give an example of the value of the timestamp you are trying to interpret)

蓝戈者 2024-12-13 03:25:37

您可以将 [timeStampString doubleValue] 直接传递到 dateWithTimeIntervalSince1970:,而不是将其转换为 NSTimeInterval

另外,尝试添加一些 NSLog 语句来查看不同点的值,这应该有助于找出差异所在。

希望这有帮助!

You can pass [timeStampString doubleValue] directly into dateWithTimeIntervalSince1970: instead of converting it into an NSTimeInterval.

Also, try adding some NSLog statements to see what your values are at various points, that should help track down where the difference is.

Hope this helps!

残花月 2024-12-13 03:25:37

使用 NSInterval 获取间隔时,请确保时间戳以秒而不是毫秒为单位。如果以毫秒为单位,只需将 NSInterval 值除以 1000,然后尝试格式化。

While using NSInterval to get the interval, make sure the timestamp is in seconds and not milliseconds. If it is in milliseconds, just divide the NSInterval value by 1000 and then try formatting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文