ios - Strange reaction when comparing dates with currentDate Swift -


i have title says strange reaction when compare dates currentdate. here happen: query server take dates print them sure took them right (and ok). print currentdate appearing 3 hours earlier. ok fix i'm saying. but! when i'm trying compare them take dates 20:59 , earlier.

this code (//dateevent dates recover server)

if dateevent.earlierdate(self.currentdate).isequaltodate(self.currentdate){    print("all dates \(dateevent)")    if nscalendar.currentcalendar().isdate(dateevent, equaltodate: self.currentdate, tounitgranularity: .day){      print("here passed server \(dateevent)")      print("here current date \(self.currentdate)")                          }                      } 

this output

all dates 2016-07-28 19:00:00 +0000 here passed server 2016-07-28 19:00:00 +0000 here current date 2016-07-28 13:43:51 +0000  dates 2016-07-28 19:00:00 +0000 here passed server 2016-07-28 19:00:00 +0000 here current date 2016-07-28 13:43:51 +0000  dates 2016-07-28 21:00:00 +0000 dates 2016-07-28 21:00:00 +0000 dates 2016-07-28 23:30:00 +0000 dates 2016-07-29 21:00:00 +0000 dates 2016-07-29 22:30:00 +0000 dates 2016-07-29 23:00:00 +0000 dates 2016-07-29 23:00:00 +0000 dates 2016-07-29 23:30:00 +0000 dates 2016-07-30 21:00:00 +0000 

i'm not going discuss time zones should use on server side, convenient , consistent way utc. you're using utc time zones on server, need take account when storing dates , times on server. mean this, if you're storing e.g. 2016-07-28 21:00:00 +0000, doesn't translate 2016-07-28 21:00:00 @ location.

see following:

let time = nsdate()        // create object current time.  print(time, "\n")          // sample output:                                2016-07-28 15:23:02 +0000 

the time object printed out outputs current time in utc. reference, local time zone me happens utc+3, local time 2016-07-28 18:23:02 +0300.

let's @ few dates in string format next:

let strings = [     "2016-07-28 19:00:00 +0000",     "2016-07-28 20:00:00 +0000",     "2016-07-28 20:59:59 +0000", // second before midnight, utc+3.     "2016-07-28 21:00:00 +0000", // midnight in utc+3.     "2016-07-28 22:00:00 +0000",     "2016-07-28 23:30:00 +0000",     "2016-07-28 23:59:59 +0000", // second before midnight, utc.     "2016-07-29 00:00:00 +0000"  // midnight in utc. ] 

and now, let's convert strings nsdate objects, again, remain in utc time zone:

var dates: [nsdate] = []  string in strings {     let formatter = nsdateformatter()     formatter.dateformat = "yyyy-mm-dd hh:mm:ss z"     guard let date = formatter.datefromstring(string) else { continue }     dates.append(date) } 

next, we'll convert nsdate objects strings local format, confusion stems from:

for date in dates {     print("current time in utc: ", time)     print("date in utc:         ", date)      let formatter = nsdateformatter()     formatter.dateformat = "yyyy-mm-dd hh:mm:ss"     formatter.timezone = nstimezone.localtimezone()     print("current local time:  ", formatter.stringfromdate(time))     print("date in local time:  ", formatter.stringfromdate(date)) }  // sample output date 2016-07-28 19:00:00 +0000: // current time in utc:  2016-07-28 15:23:02 +0000 // date in utc:          2016-07-28 19:00:00 +0000 // current local time:   2016-07-28 18:23:02 // date in local time:   2016-07-28 22:00:00 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -