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,
Did you check that the string (
timestampString) and the double value (_interval) are the same, and that thedoubleValuedoes 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)