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

73 Views Asked by At

I am getting timestamp for server in stringFormat which is in 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. But when I run it in iPhone 5 and iPhone5s it shows me unwanted date.

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

I am in doubt that NSTimeinterval is overflowed with data. is this right?

How can I fix this.

1

There are 1 best solutions below

0
Jigar On BEST ANSWER

try this code

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