views:

65

answers:

1

Hello,

in my application I get, using JSON, a timestamp value like this :

  • 1278321016000 for 2010-07-05 11:10:16.0 CEST
  • 1278436867000 for 2010-07-06 19:21:07.0 CEST

I'm currently storing this value to long type, but I wonder if it is the right way, have I to search thing with NSTimeInterval ?

What would be, after storing this value, the best way to show this in a UILabel object, looking like "YYYY-MM-DD hh:mm:ss" ?

I tried to use NSDate object, but I can't get the initWithTimeIntervalSince1970 method...

Thank you in advance !

+2  A: 

Note that NSTimeInterval uses seconds, not milliseconds. Here is some code to create the date and display it. I haven't put this in to XCode and run this, so please excuse any errors.

// convert to a usable time interval
NSTimeInterval timeInterval = 1278321016;

// convert the time interval to a date
NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

// create the formatter for the desired output
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

// set the label text
myLabel.text = [formatter stringFromDate:myDate];

// cleanup
[formatter release];
rchern
Thanks for your answer, what is timeInterval value type ?I'm trying to divide the value I get by 1000 but I have strange value... surely a type mistake.
Dough
Are you assigning the result of the division to NSTime Interval or are you doing division on the NSTimeInterval? NSTimeInterval: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html#jumpTo_77
rchern
That's good at this time !I have to initialize timeInterval like this : NSTimeInterval timeInterval = [myData unsignedLongLongValue]/1000;Thank you for your help !
Dough
@ rchem :Put a ; after [formatter release]
raaz
Yikes! Thanks raaz!
rchern