Storing a timestamp value and showing it to the UI
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开发者_JAVA技巧 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 !
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];
精彩评论