Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert long-integer time to NSString or NSDate to display time?

I got a serial number form Java Date which convert into long-integer such as "1352101337000". The problem I met is how to analyze this long-integer number back to NSDate or NSString so that I can clear to know what time the serial number is displaying.

Do anybody have solution for this case?

like image 750
Lothario Avatar asked Nov 05 '12 08:11

Lothario


1 Answers

Use this,

NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSince1970];

To change it back,

NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeInMiliseconds];

As per apple documentation,

NSTimeInterval: Used to specify a time interval, in seconds.

typedef double NSTimeInterval;

It is of type double.

To convert a date to string,

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];

//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];

NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];

[formatter release];
like image 99
iDev Avatar answered Oct 06 '22 02:10

iDev