Reputation: 2621
Because this post is related to comparing a given date to "today's date" keep in mind that I am posting this on a 2017-01-26
Basically, I am doing the following:
However, it seems like I am doing something wrong, or missing something since the output seems abnormal.
I copied this output from my console:
String sent by the server --> 2017-1-27T0:00:00.000Z
Conversion I make with a dateFormatter --> (2017-01-27 00:00:00 +0000)
2017-01-27 00:00:00 +0000 is TODAY
The output above is obviously wrong. Because that is tomorrow, not today.
Now, in the following output, the date sent by the server is actually today but NSCalendar.currentCalendar().isDateInToday(myDate!) says otherwise
String sent by the server --> 2017-1-26T0:00:00.000Z
Conversion I make with a dateFormatter Optional(2017-01-26 00:00:00 +0000)
2017-01-26 00:00:00 +0000 is NOT TODAY
My question is, what could I be doing wrong? I've double checked that my computer, the simulator and the dateFormatter.locale are properly set.
I've tried locales in string like "es_CL" and also
dateFormatter.locale = NSLocale.currentLocale()
Upvotes: 3
Views: 2562
Reputation: 130092
This is probably about the timezone. Your server returns dates is UTC
, that is GMT-00:00
.
If you are in Chile, your local timezone is -3 hours. Therefore today actually starts at 2017-01-26 03:00:00 +0000
and ends at 2017-01-27 03:00:00 +0000
.
In that respect, the result you are seeing is completely correct. The returned daytime is not "today" in your local timezone.
To change this behavior, you can set the timezone on your calendar to UTC:
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = TimeZone(secondsFromGMT: 0)
Upvotes: 9