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

I am really trying to convert this timestamp into a nice formatted date string.

Here's the timestamp: "1316625985"

And here is the function I 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];
}

The problem is that it keeps return dates in 1972! (31/7/72) This is wrong, as it is a September date ...

Can anyone suggest any solution?

Thank you very much in advance,

+5
source share
5 answers

Have you checked that string ( timestampString) and double value ( _interval) match, and what doubleValuereally takes into account all the characters of your string?

, ?


, , , UNIX, , 01.01.1970 ( 01.01.1970?... , ?)

(, , )

+2

epoch 01.01.1970? , ?

, ( append 000 ms), . :

    NSString * timeStampString = @"1316641549";
    NSTimeInterval _interval=[timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSLog(@"%@", date);

:

2011-09-21 17: 46: 14.384 Craplet [13218: 707] 2011-09-21 21:45:49 +0000

+14

[timeStampString doubleValue] , :

NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue]/1000.0];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:@"dd/MM/yy"];
return [_formatter stringFromDate:date];

[timeStampString longLongValue]/1000.0

!

+5

[timeStampString doubleValue] dateWithTimeIntervalSince1970: , NSTimeInterval.

, NSLog, , , , .

, !

+2

NSInterval , , . , NSInterval 1000, .

+1

All Articles