0

I am having two problems. Any help with any of the issues will be really appreciated.

First - I am not getting the right NSDate from my device to start with but apparently there is nothing I can do about it.

Second - When I try to convert nsDateAsString to nsDateFromString it never matches.

One thing I noticed was that the daylight-savings is enabled. Will it have any influence on my NSDate? How do I manipulate that?

Code example

   func NSDateToTimestamp(nsDate: NSDate) -> Double{

        print("------------START-------------")
        print("nsDate \(nsDate)")

        let formater = NSDateFormatter()
        formater.dateFormat = "yyyy-MM-dd HH:mm:ss"
        // formater.timeZone = NSTimeZone.systemTimeZone()
        // formater.timeZone = NSTimeZone.localTimeZone()
        // formater.timeZone = NSTimeZone(forSecondsFromGMT: 3600)

        print ("DLS offset \(formater.timeZone.daylightSavingTimeOffset)")
        print ("DLS on/off \(formater.timeZone.daylightSavingTime)")

        print("-------------------------------")


        // string from date
        let nsDateAsString = formater.stringFromDate(nsDate)
        print("nsDateAsString \(nsDateAsString)")

        // get format back to nsdate
        let nsDateFromString = formater.dateFromString(nsDateAsString)
        print("nsDateFromString \(nsDateFromString!)")

        // convert nsdate to timestamp
        let nsDateConvertedToTimestamp = nsDateFromString!.timeIntervalSince1970
        print("nsDateConvertedToTimestamp \(nsDateConvertedToTimestamp)")

        // convert timestamp to NSDate
        let timestampConvertedBackToNSDate = NSDate(timeIntervalSince1970: nsDateConvertedToTimestamp)
        print("timestampConvertedBackToNSDate \(timestampConvertedBackToNSDate)")

        return nsDateConvertedToTimestamp
    }

My output ALL DISABLED while it is 4 Feb - 8:11 AM on my phone.

------------START-------------
nsDate 2016-02-03 19:33:22 +0000
DLS offset 3600.0
DLS on/off true
-------------------------------
nsDateAsString 2016-02-04 08:33:22
nsDateFromString 2016-02-03 19:33:22 +0000
nsDateConvertedToTimestamp 1454528002.0
timestampConvertedBackToNSDate 2016-02-03 19:33:22 +0000

My output NSTimeZone.localTimeZone() ENABLED while it is 4 Feb - 8:32 AM on my phone.

------------START-------------
nsDate 2016-02-03 19:32:07 +0000
DLS offset 3600.0
DLS on/off true
-------------------------------
nsDateAsString 2016-02-04 08:32:07
nsDateFromString 2016-02-03 19:32:07 +0000
nsDateConvertedToTimestamp 1454527927.0
timestampConvertedBackToNSDate 2016-02-03 19:32:07 +0000

My output NSTimeZone.systemTimeZone() ENABLED while it is 4 Feb - 8:32 AM on my phone.

------------START-------------
nsDate 2016-02-03 19:32:42 +0000
DLS offset 3600.0
DLS on/off true
-------------------------------
nsDateAsString 2016-02-04 08:32:42
nsDateFromString 2016-02-03 19:32:42 +0000
nsDateConvertedToTimestamp 1454527962.0
timestampConvertedBackToNSDate 2016-02-03 19:32:42 +0000

My output NSTimeZone(forSecondsFromGMT: 3600) ENABLED while it is 4 Feb - 8:31 AM on my phone.

------------START-------------
nsDate 2016-02-03 19:31:16 +0000
DLS offset 0.0
DLS on/off false
-------------------------------
nsDateAsString 2016-02-03 20:31:16
nsDateFromString 2016-02-03 19:31:16 +0000
nsDateConvertedToTimestamp 1454527876.0
timestampConvertedBackToNSDate 2016-02-03 19:31:16 +0000

My output NSTimeZone(forSecondsFromGMT: 0) ENABLED while it is 4 Feb - 8:30 AM on my phone.

------------START-------------
nsDate 2016-02-03 19:30:09 +0000
DLS offset 0.0
DLS on/off false
-------------------------------
nsDateAsString 2016-02-03 19:30:09
nsDateFromString 2016-02-03 19:30:09 +0000
nsDateConvertedToTimestamp 1454527809.0
timestampConvertedBackToNSDate 2016-02-03 19:30:09 +0000
GuiSoySauce
  • 1,763
  • 3
  • 24
  • 37
  • Where the duplicate is for Objective-C, it answers your question completely. Briefly, `NSDate` stores internally in UTC, `NSDate.toString` prints the time in UTC. `NSDateFormatter` and your format are going to use the correct local time zone. – David Berry Feb 02 '16 at 23:24
  • I reformulated the question. Let me know if that helps. – GuiSoySauce Feb 03 '16 at 00:29
  • Most of the perceived troubles with `NSDate` is due to timezone. I've posted on that too may times on here to count. Think about your local timezone vs. GMT for a moment – Code Different Feb 03 '16 at 02:10
  • Had a play with timezone and GMT but could not figure it out. I've updated the question with more outputs and experiments. – GuiSoySauce Feb 03 '16 at 19:28
  • @GuiSoySauce What timezone are you in? – Code Different Feb 03 '16 at 19:51
  • New Zealand Standard Time (NZST) is 12 hours ahead of Coordinated Universal Time. I could just manually force my timestamp but someone using the app in another timezone would get a completely useless timestamp. Really don't know what to do... really odd. – GuiSoySauce Feb 04 '16 at 01:10

0 Answers0