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?
Asked
Active
Viewed 5.8k times
44

KingofBliss
- 15,055
- 6
- 50
- 72

ramya
- 451
- 1
- 4
- 3
5 Answers
147
OBJ - C: Use dateWithTimeIntervalSince1970:,
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
SWIFT: Use init(timeIntervalSince1970:)
let date = NSDate(timeIntervalSince1970:timeStamp)

KingofBliss
- 15,055
- 6
- 50
- 72
-
3Depending on the typecasting of timeStamp. Else you could try it like this: NSDate *beginDate = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[[calendarData objectForKey:@"date"] doubleValue]]; – Leon Feb 13 '13 at 11:44
-
i am also doing it same in swift but getting wrong date..which part i missed it. var transDate: Double =transDict.valueForKey("date") as! Double var tranDate = NSDate(timeIntervalSince1970: transDate) let dateFormatter = NSDateFormatter() dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle dateFormatter.dateFormat = "EEE, MMM dd, y" dateFormatter.timeZone = NSTimeZone(abbreviation:"CST") let dateValue = dateFormatter.stringFromDate(tranDate) @KingofBliss – simbesi.com May 18 '15 at 13:17
18
You can use the use the method dateWithTimeIntervalSince1970:
to convert the timestamp you've which is the epoch time.
NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:1308031456];

Deepak Danduprolu
- 44,595
- 12
- 101
- 105
5
In obj c
double timeStamp = 1513330403393;
NSTimeInterval unixTimeStamp = timeStamp / 1000.0;
NSDate *exactDate = [NSDate dateWithTimeIntervalSince1970:unixTimeStamp];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd/MM/yyy hh:mm a";
NSString *finalate = [dateFormatter stringFromDate:exactDate];
In Swift
let timeStamp = 1513330403393
let unixTimeStamp: Double = Double(timeStamp) / 1000.0
let exactDate = NSDate.init(timeIntervalSince1970: unixTimeStamp)
let dateFormatt = DateFormatter();
dateFormatt.dateFormat = "dd/MM/yyy hh:mm a"
print(dateFormatt.string(from: exactDate as Date))

vivek
- 248
- 3
- 6
-
Please note that code only answers are discouraged. Always add a bit of explanation ... for example not using comments like this - instead put that into well formatted text! – GhostCat Dec 19 '17 at 09:11
-
probably a good idea to just use Date now for swift, also instead of using .init just open `Date(timeIntervalSince1970: unixTimeStamp)` will do – James Wolfe Feb 14 '19 at 16:24
2
See the various example on date formatting and getting date from timestamp from apple
Use Formatter Styles to Present Dates and Times With the User’s Preferences

Jhaliya - Praveen Sharma
- 31,697
- 9
- 72
- 76
0
Here is a code piece which lets you convert the unix type timestamp to Formatted Date
NSString * timeStampString =@"1304245000";
NSTimeInterval _interval=[timeStampString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:@"dd.MM.yyyy"];
NSString *_date=[_formatter stringFromDate:date];
This will print _date something like 05.03.2011

incredever
- 231
- 1
- 2
- 11