ios - How to check if two NSDates are from the same day -
this question has answer here:
- comparing nsdates without time component 14 answers
i working on ios development , find hard check if 2 nsdates same day. tried use this
fetchdatelist() // check date let date = nsdate() // setup date formatter let dateformatter = nsdateformatter() // set current time zone dateformatter.locale = nslocale.currentlocale() let latestdate = datalist[datalist.count-1].valueforkey("representdate") as! nsdate //let newdate = dateformatter.stringfromdate(date) let diffdatecomponent = nscalendar.currentcalendar().components([nscalendarunit.year, nscalendarunit.month, nscalendarunit.day], fromdate: latestdate, todate: date, options: nscalendaroptions.init(rawvalue: 0)) print(diffdatecomponent.day)
but checks if 2 nsdates has difference of 24 hours. think there way make work still, wish have nsdate values before 2 in morning count day before, need here. thanks!
nscalendar has method want actually!
/* api compares days of given dates, reporting them equal if in same day. */ - (bool)isdate:(nsdate *)date1 insamedayasdate:(nsdate *)date2 ns_available(10_9, 8_0);
so you'd use this:
[[nscalendar currentcalendar] isdate:date1 insamedayasdate:date2];
or in swift
calendar.current.isdate(date1, insamedayas:date2)
Comments
Post a Comment