swift - Two NSDates created next to each other do not compare equal -


this code, comparing 2 nsdates 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

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -