Converting Milliseconds to Dates

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting Milliseconds to Dates

Rob Vig
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Milliseconds to Dates

Swift Language mailing list
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

> On 11 Jun 2015, at 16:00, Rob Vig <[hidden email]> wrote:
>
> 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
>
> --
> 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 [hidden email].
> To post to this group, send email to [hidden email].
> 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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/8E7A48BD-A403-4A07-8DC8-6193BFD12A56%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Milliseconds to Dates

Marco S Hyman
In reply to this post by Rob Vig

> "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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/70C35AA0-2D21-4326-8EFD-16D367C1C2C7%40snafu.org.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Milliseconds to Dates

Rob Vig
In reply to this post by Swift Language mailing list
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.

On Thursday, June 11, 2015 at 11:18:22 AM UTC-4, Jeremy Pereira wrote:
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

> On 11 Jun 2015, at 16:00, Rob Vig <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="KYaiX-fbtqwJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">rob...@...> wrote:
>
> 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
>
> --
> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="KYaiX-fbtqwJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">swift-languag...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="KYaiX-fbtqwJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">swift-l...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/swift-language/67924a59-054b-415c-be23-c4689116db57%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/msgid/swift-language/67924a59-054b-415c-be23-c4689116db57%40googlegroups.com';return true;" onclick="this.href='https://groups.google.com/d/msgid/swift-language/67924a59-054b-415c-be23-c4689116db57%40googlegroups.com';return true;">https://groups.google.com/d/msgid/swift-language/67924a59-054b-415c-be23-c4689116db57%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/019d4ea0-d3a7-4bd9-9d0e-aebfbf422014%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Milliseconds to Dates

Rob Vig
In reply to this post by Marco S Hyman
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...



On Thursday, June 11, 2015 at 11:53:03 AM UTC-4, Marco Hyman wrote:

> "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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/33eca98b-c2f6-4854-b7b4-32894c9a6bc3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Milliseconds to Dates

Swift Language mailing list
In reply to this post by Rob Vig

> On 11 Jun 2015, at 16:59, Rob Vig <[hidden email]> 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)

>
> On Thursday, June 11, 2015 at 11:18:22 AM UTC-4, Jeremy Pereira wrote:
> 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
>
> > On 11 Jun 2015, at 16:00, Rob Vig <[hidden email]> wrote:
> >
> > 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
> >
> > --
> > 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 [hidden email].
> > To post to this group, send email to [hidden email].
> > 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.
>
>
> --
> 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 [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/019d4ea0-d3a7-4bd9-9d0e-aebfbf422014%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/D6A97727-B063-4CED-9731-B0552845124E%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Milliseconds to Dates

Rob Vig
In reply to this post by Rob Vig


    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)"

            }

        }

    }







On Thursday, June 11, 2015 at 11:00:46 AM UTC-4, Rob Vig wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/c33eae42-c525-4293-8fd8-e2318599d5db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Converting Milliseconds to Dates

Rob Vig

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


On Thursday, June 11, 2015 at 2:24:18 PM UTC-4, Rob Vig wrote:


    var time: String = "" {

        didSet {

            if (time != oldValue) {

                

                

                // Playground: "Milliseconds.playground

                // <a href="http://stackoverflow.com/questions/30109219/nsdate-timeintervalsince1970-not-working-in-swift" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fquestions%2F30109219%2Fnsdate-timeintervalsince1970-not-working-in-swift\46sa\75D\46sntz\0751\46usg\75AFQjCNGstpETeAZl9_CVFIs2me-0v1Z3ig';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fquestions%2F30109219%2Fnsdate-timeintervalsince1970-not-working-in-swift\46sa\75D\46sntz\0751\46usg\75AFQjCNGstpETeAZl9_CVFIs2me-0v1Z3ig';return true;">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()

                // <a href="http://www.codingexplorer.com/swiftly-getting-human-readable-date-nsdateformatter/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fwww.codingexplorer.com%2Fswiftly-getting-human-readable-date-nsdateformatter%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGWkMNon7L-zJxxQ681NQBTfGceCg';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fwww.codingexplorer.com%2Fswiftly-getting-human-readable-date-nsdateformatter%2F\46sa\75D\46sntz\0751\46usg\75AFQjCNGWkMNon7L-zJxxQ681NQBTfGceCg';return true;">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)"

            }

        }

    }







On Thursday, June 11, 2015 at 11:00:46 AM UTC-4, Rob Vig wrote:
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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/7bca1de2-50a1-4e95-bdcc-a08a4b45d887%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.