11

I have this "1273636800000" and I want to convert it to this format "Wed Mar 03 2010 00:00:00 GMT-0500 (EST)"

I need to convert this milliseconds to NSDate format.

I tried this

NSDate *tr = [NSDate dateWithTimeIntervalSince1970:1273636800000];

and

NSDate *tr = [NSDate dateWithTimeIntervalSinceReferenceDate:1273636800000];

and

NSDate *tr = [NSDate dateWithTimeIntervalSinceNow:1273636800000];

But I can't get it to work, anyone had a similar problem and found the solution

Mohamed Elkassas
  • 829
  • 1
  • 13
  • 33
vivianaranha
  • 2,781
  • 6
  • 33
  • 47

3 Answers3

31

These functions (dateWithTimeIntervalSince1970, dateWithTimeIntervalSinceReferenceDate, dateWithTimeIntervalSinceNow) accept parameter in seconds.

NSDate *date = [NSDate dateWithTimeIntervalSince1970:(ms / 1000.0)];

You should pass 1273636800.000

NSDate *date = [NSDate dateWithTimeIntervalSince1970:(1273636800 / 1000.0)];
Chanchal Raj
  • 4,176
  • 4
  • 39
  • 46
Victor
  • 3,497
  • 3
  • 39
  • 51
  • I spent a week looking around for this, if you think 3 hours is dumb, what should I call myself? XD – SAFAD Sep 12 '13 at 19:55
5

Divide by 1000 to get millis in seconds, that's what you want.

tisaksen
  • 329
  • 4
  • 10
4

A simple one-line Swift 2 function:

func dateFromMilliseconds(ms: NSNumber) -> NSDate {
    return NSDate(timeIntervalSince1970:Double(ms) / 1000.0)
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220