Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting timestamp to nsdate format

I want to convert my timestamp value 1308031456 to NSDate format (which yields the value Tue, 14 Jun 2011 06:04:16 GMT in online web conversion). How would I convert between the two programmatically?

like image 566
ramya Avatar asked Jun 20 '11 05:06

ramya


People also ask

How to convert timestamp to date in Objective c?

use this: NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000]; then convert your date in the format you want.

How do I convert timestamps to dates?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.


2 Answers

OBJ - C: Use dateWithTimeIntervalSince1970:,

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp]; 

SWIFT: Use init(timeIntervalSince1970:)

let date = NSDate(timeIntervalSince1970:timeStamp) 
like image 100
KingofBliss Avatar answered Oct 05 '22 17:10

KingofBliss


You can use the use the method dateWithTimeIntervalSince1970: to convert the timestamp you've which is the epoch time.

NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:1308031456]; 
like image 29
Deepak Danduprolu Avatar answered Oct 05 '22 18:10

Deepak Danduprolu