mike vorisis mike vorisis - 3 months ago 10
iOS Question

Strange reaction when comparing dates with currentDate Swift

I have as the title says a very strange reaction when I compare dates with currentDate.
Here is what happen:
I do a query to my server to take some dates then I print them so I will be sure that it took them right (and everything is ok).
Then I print currentDate which is appearing with 3 hours earlier.
Ok I will fix it then I'm saying.
BUT! when I'm trying to compare them I take only the dates that are 20:59 and earlier.

This is my code (

//dateevent are the dates that I recover from 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 is what it passed from server \(dateevent)")
print("here is the current date \(self.currentDate)")
}

}


This is my output

All dates 2016-07-28 19:00:00 +0000
here is what it passed from server 2016-07-28 19:00:00 +0000
here is the current date 2016-07-28 13:43:51 +0000

All dates 2016-07-28 19:00:00 +0000
here is what it passed from server 2016-07-28 19:00:00 +0000
here is the current date 2016-07-28 13:43:51 +0000

All dates 2016-07-28 21:00:00 +0000
All dates 2016-07-28 21:00:00 +0000
All dates 2016-07-28 23:30:00 +0000
All dates 2016-07-29 21:00:00 +0000
All dates 2016-07-29 22:30:00 +0000
All dates 2016-07-29 23:00:00 +0000
All dates 2016-07-29 23:00:00 +0000
All dates 2016-07-29 23:30:00 +0000
All dates 2016-07-30 21:00:00 +0000

Answer

I finally found the answer of my question!

I found it here

What I did is this:

let utcCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)

Inside viewDidLoad I pu this:

utcCalendar!.timeZone = NSTimeZone(abbreviation: "UTC")!

and finnaly in my statement i put this:

if self.utcCalendar!.isDate and so on...

Thank you all for your answers!

Comments