swift - Two NSDates created next to each other do not compare equal -
this code, comparing 2 nsdate
s created 1 after other, not enter .orderedsame
case expect.
import uikit let chosendate = nsdate() let currentdate = nsdate() let formatter = nsdateformatter() formatter.dateformat = "eeee" let day = formatter.stringfromdate(chosendate) var result = string() switch chosendate.compare(currentdate) { case .orderedsame: result = "today \(day)" break case .orderedascending: result = "that \(day)" break case .ordereddescending: result = "that \(day)" break } print(result)
whenever run (in playground or [i pulled playground project file test stuff] in simulator) gives me "that was (insert day here) instead of "today (insert day here). why this?
the values of chosendate
, currentdate
not same. if print values of dates, see different, few milliseconds.
if want check if days same, need try nscalendar
method comparedate:todate:tounitgranularity:
you can use nscalendarunitday
compare if dates on same day.
Comments
Post a Comment