Converting Milliseconds to Dates

518 views
Skip to first unread message

Rob Vig

unread,
Jun 11, 2015, 11:00:46 AM6/11/15
to swift-l...@googlegroups.com
Thank you for reading my post!

I've got a JSON "time" element that looks like:   "time":1434003904260

"time" is a 'long integer' and represented in milliseconds since the epoch (1970-01-01T00:00:00.000Z)

I need to convert this to a "normal" date. I'm using Int32 and I've gone through all kinds of gyrations and I can't get it to work.

I always come up with something like:  -516372864

I'm hoping someone with more experience with Swift and "Time" can help me solve this.

Thank you!  Robert

Jeremy Pereira

unread,
Jun 11, 2015, 11:18:22 AM6/11/15
to Rob Vig, swift-l...@googlegroups.com
When I do this in a playground

let foo: NSTimeInterval = 1434003904260.0/1000
let theDate = NSDate(timeIntervalSince1970: foo)

I am told that it is 11 Jun 2015 07:25

That's probably in UTC because NSDates represents instants in time rather than dates in a particular calendar and time zone. To get a date for your locale and calendar, you need things like NSCalendars and NSDateFormatters
> --
> You received this message because you are subscribed to the Google Groups "Swift Language" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
> To post to this group, send email to swift-l...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/67924a59-054b-415c-be23-c4689116db57%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Marco S Hyman

unread,
Jun 11, 2015, 11:53:03 AM6/11/15
to Rob Vig, swift-l...@googlegroups.com

> "time" is a 'long integer' and represented in milliseconds since the epoch (1970-01-01T00:00:00.000Z)
>
> I need to convert this to a "normal" date. I'm using Int32 and I've gone through all kinds of gyrations and I can't get it to work.

Milliseconds since the Unix epoch is a number larger than can fit in a 32 bit
variable.

2015-06-05T17:16:37Z = 1,433,524,597,000 milliseconds which is a lot larger
than 2,147,483,647 (Int32.max)

Marc

Rob Vig

unread,
Jun 11, 2015, 11:59:59 AM6/11/15
to swift-l...@googlegroups.com, rob...@gmail.com
Thanks Jeremy! I'm working on "tweaking" your coded a bit. I'm having difficulty parsing the JSON as NSTimeInterval and Int32 won't let me grab the value.

Rob Vig

unread,
Jun 11, 2015, 12:01:16 PM6/11/15
to swift-l...@googlegroups.com, rob...@gmail.com
Hey Marco! Thanks for responding!

Yeah... I found that out (limit of Int32 and the number of ticks)
 I'm having difficulty parsing the original JSON value as NSTimeInterval and the trying to cast that value...

Jeremy Pereira

unread,
Jun 11, 2015, 12:25:24 PM6/11/15
to Rob Vig, swift-l...@googlegroups.com

> On 11 Jun 2015, at 16:59, Rob Vig <rob...@gmail.com> wrote:
>
> Thanks Jeremy! I'm working on "tweaking" your coded a bit. I'm having difficulty parsing the JSON as NSTimeInterval and Int32 won't let me grab the value.

NSTimeInterval is an alias for Double. Assuming you get the time as an Int (which is 64 bits), this will do the trick

let timeAsInt: Int = 1434003904260

let timeAsInterval: NSTimeInterval = Double(timeAsInt)/1000

let theDate = NSDate(timeIntervalSince1970: timeAsInterval)
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/019d4ea0-d3a7-4bd9-9d0e-aebfbf422014%40googlegroups.com.

Rob Vig

unread,
Jun 11, 2015, 2:24:18 PM6/11/15
to swift-l...@googlegroups.com


    var time: String = "" {

        didSet {

            if (time != oldValue) {

                

                

                // Playground: "Milliseconds.playground

                // http://stackoverflow.com/questions/30109219/nsdate-timeintervalsince1970-not-working-in-swift

                

                //var jsonStr:String = "1434003904260"

                var jsonStr:String = time

                var intValue:Int = NSString(string: jsonStr).integerValue

                let ti = NSTimeInterval(intValue)

                

                let date = NSDate(timeIntervalSince1970: ti/1000.0)

                //println("date is \(date)")

                

                let formatter = NSDateFormatter()

                // http://www.codingexplorer.com/swiftly-getting-human-readable-date-nsdateformatter/

                formatter.dateStyle = NSDateFormatterStyle.LongStyle

                formatter.timeStyle = .MediumStyle

                //println("formatted date is \(formatter.stringFromDate(date))")

                timeLabel.text = "\(date)"

Rob Vig

unread,
Jun 11, 2015, 2:51:54 PM6/11/15
to swift-l...@googlegroups.com

timeLabel.text = "\(formatter.stringFromDate(date))"

Reply all
Reply to author
Forward
0 new messages