简体   繁体   English

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

[英]Objective-C: help me convert a timestamp to a formatted date/time

I'm really struggling to convert this timestamp to a nice formatted date string. 我真的很难将这个时间戳转换为一个很好的格式化日期字符串。

Here's the timestamp: "1316625985" 这是时间戳:“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! 麻烦的是,它在1972年保持返回日期! (31/7/72) This is wrong, since it's a September 2011 date... (31/7/72)这是错误的,因为它是2011年9月的日期......

Can anyone suggest any solution? 有谁能建议任何解决方案?

Many thanks in advance, 提前谢谢了,

Isn't epoch seconds since 1/1/1970? 不是自1970年1月1日以来的纪元 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. 当我运行你的逻辑(没有你的行为ms附加000)时,它工作。 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 2011-09-21 17:46:14.384 Craplet [13218:707] 2011-09-21 21:45:49 +0000

Just divide your [timeStampString doubleValue] by a thousand like: 只需将你的[timeStampString doubleValue]除以千:

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 或者尝试[timeStampString longLongValue] /1000.0

Good Luck! 祝好运!

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? 您是否检查过字符串( timestampString )和double值( _interval )是否相同,而doubleValue是否确实考虑了字符串中的所有字符?

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?) 你是否也确定你所解释的时间戳实际上是一个UNIX时间戳,这意味着它计算自1970年1月1日以来经过的秒数(而不是1970年1月1日以来的天数?......或者不是秒,但是从另一个日期开始? )

(Maybe give an example of the value of the timestamp you are trying to interpret) (也许举一个你试图解释的时间戳值的例子)

You can pass [timeStampString doubleValue] directly into dateWithTimeIntervalSince1970: instead of converting it into an NSTimeInterval . 您可以将[timeStampString doubleValue]直接传递给dateWithTimeIntervalSince1970:而不是将其转换为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. 此外,尝试添加一些NSLog语句,以查看您的值在不同点上的值,这应该有助于追踪差异的位置。

Hope this helps! 希望这可以帮助!

While using NSInterval to get the interval, make sure the timestamp is in seconds and not milliseconds. 使用NSInterval获取间隔时,请确保时间戳以秒为单位而不是毫秒。 If it is in milliseconds, just divide the NSInterval value by 1000 and then try formatting. 如果是以毫秒为单位,只需将NSInterval值除以1000,然后尝试格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM