简体   繁体   English

使用Objective-C将时间戳解析为日期和时间时出现的问题

[英]Issue while parsing timestamp into date and time using Objective-C

I am getting timestamp for server in stringFormat which is in miliseconds . 我正在使用stringFormat获取服务器的timestamp ,以miliseconds

I have to convert it in date and time and display to the user. 我必须将其转换为日期和时间,并显示给用户。

I have done it using following code: 我已经使用以下代码完成了它:

    + (NSString *)getDateAndMonthFromTimeStamp:(NSString*)timestampString
{
NSTimeInterval timeStamp = [timestampString integerValue]/1000;

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];

[dateFormatter setDateFormat:@"dd MMM"];

 NSString *dateNmonth =  [[dateFormatter stringFromDate:date]uppercaseString];

return dateNmonth;
}

When I run it in iPhone6 it works fine. 当我在iPhone6中运行它时,它工作正常。 But when I run it in iPhone 5 and iPhone5s it shows me unwanted date. 但是,当我在iPhone 5和iPhone5s中运行它时,它显示了我不需要的日期。

I debug the code and found this: 我调试了代码,发现了这一点:

  timestampString = @"1459498716000"

  NSTimeInterval timeStamp = [timestampString integerValue]/1000; 
 //after this timeStamp becomes:
  timeStamp = 2147483;

and then my date becomes 1970-01-25 20:31:23 +0000 然后我的约会日期变为1970-01-25 20:31:23 +0000

I am in doubt that NSTimeinterval is overflowed with data. 我怀疑NSTimeinterval充满了数据。 is this right? 这是正确的吗?

How can I fix this. 我怎样才能解决这个问题。

try this code 试试这个代码

NSString* takeOffTime = @"1396614600000"; 
double miliSec = takeOffTime.doubleValue; 
NSDate* takeOffDate = [NSDate dateWithTimeIntervalSince1970:miliSec/1000]; 

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

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