Objective-C:帮助我将时间戳转换为格式化的日期/时间
我真的很难将此时间戳转换为格式良好的日期字符串。
这是时间戳:“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
纪元不是自 1970 年 1 月 1 日以来的秒吗?你确定你没有留下毫秒行吗?
当我运行你的逻辑时(没有你的行附加 000 毫秒),它起作用了。这就是我所做的:
它输出:
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:
It outputted:
2011-09-21 17:46:14.384 Craplet[13218:707] 2011-09-21 21:45:49 +0000
只需将 [timeStampString doubleValue] 除以一千即可:
或者尝试 [timeStampString longLongValue]/1000.0 祝
你好运!
Just divide your [timeStampString doubleValue] by a thousand like:
or try [timeStampString longLongValue]/1000.0 instead
Good Luck!
您是否检查过字符串 (
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 thedoubleValue
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)
您可以将
[timeStampString doubleValue]
直接传递到dateWithTimeIntervalSince1970:
,而不是将其转换为NSTimeInterval
。另外,尝试添加一些 NSLog 语句来查看不同点的值,这应该有助于找出差异所在。
希望这有帮助!
You can pass
[timeStampString doubleValue]
directly intodateWithTimeIntervalSince1970:
instead of converting it into anNSTimeInterval
.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!
使用 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.