Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert epoch time to NSDate in cocoa/iPhone

I have the value of the epoch time like say 123456789, now I want to convert this into NSDate in cocoa framework. Can anyone show me?

thanks

like image 297
Raja Avatar asked Apr 09 '10 17:04

Raja


People also ask

How do I convert epoch time to real time?

Convert from epoch to human-readable datemyString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch. =(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number).

How do I convert epoch time to seconds?

Epoch Time Difference FormulaMultiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.

How does Python calculate epoch time?

Using strftime() to convert Python datetime to epoch strftime() is used to convert string DateTime to DateTime. It is also used to convert DateTime to epoch. We can get epoch from DateTime from strftime().


2 Answers

Documentation is your friend!

NSDate* date = [NSDate dateWithTimeIntervalSince1970:123456789];
like image 189
Jason Coco Avatar answered Oct 13 '22 23:10

Jason Coco


Since this is a high hit, going to contribute. Note that just converting to NSDate will put it into the UTC timezone. Then you have to convert over to your timezone if you want to display it to the user, from Convert epoch time to NSDate with good timezone with Objective c:

// Convert NSString to NSTimeInterval
NSTimeInterval seconds = [epochTime doubleValue];

// (Step 1) Create NSDate object
NSDate *epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:seconds];
NSLog (@"Epoch time %@ equates to UTC %@", epochTime, epochNSDate);

// (Step 2) Use NSDateFormatter to display epochNSDate in local time zone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSLog (@"Epoch time %@ equates to %@", epochTime, [dateFormatter stringFromDate:epochNSDate]);

// (Just for interest) Display your current time zone
NSString *currentTimeZone = [[dateFormatter timeZone] abbreviation];
NSLog (@"(Your local time zone is: %@)", currentTimeZone);
like image 24
pfrank Avatar answered Oct 13 '22 23:10

pfrank