69

I am working on ios development and I find it really hard to check if two NSDates are from the same day. I tried to 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 it just checks if two NSDates has a difference of 24 hours. I think there is a way to make it work but still, I wish to have NSDate values before 2 am in the morning to be count as the day before, so I definitely need some help here. Thanks!

JoshJoshJosh
  • 897
  • 2
  • 11
  • 20

2 Answers2

195

NSCalendar has a method that does exactly what you want actually!

/*
    This API compares the Days of the given dates, reporting them equal if they are in the same Day.
*/
- (BOOL)isDate:(NSDate *)date1 inSameDayAsDate:(NSDate *)date2 NS_AVAILABLE(10_9, 8_0);

So you'd use it like this:

[[NSCalendar currentCalendar] isDate:date1 inSameDayAsDate:date2];

Or in Swift

Calendar.current.isDate(date1, inSameDayAs:date2)
Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
  • 1
    Minor Update: In Swift 3.0 Xcode 8.2 Syntax: `if NSCalendar.current.isDate(date1, inSameDayAs:date2) == true) { // do your stuff}` – KuboAndTwoStrings Jan 08 '17 at 19:55
  • 3
    It seems to not work for specific dates like `2017-06-12T00:00:00.000Z` and `2017-06-12T23:59:59.999Z` --> "23:59:59" falls into next day due to some reason (my timezone is +2, and 21:59:59 works fine). Seems to do something with timezones, but I haven't figured how to make it work. – Sergii N. Jun 13 '17 at 13:45
  • Is that a DST transition? If so that day may only have 23 hours. – Catfish_Man Jun 13 '17 at 18:55
  • @Catfish_Man Do you have any thoughts about workaround for DST transition? I am still not able to find working solution for this edge case. – Sergii N. Jul 03 '17 at 19:47
  • 1
    Is there something to work around? What I described is correct behavior; the day really does end an hour early once per year. – Catfish_Man Jul 03 '17 at 21:09
  • 1
    Apart from the obvious convenience of not having to write your own code, the built in method is pleasingly fast, almost twice as fast as comparing date components. – lewis Nov 06 '21 at 17:06
  • @Catfish_Man please, could you confirm, that here https://www.zerotoappstore.com/how-to-check-if-two-dates-are-from-the-same-day-swift.html is wrong approach? – Bartłomiej Semańczyk Nov 30 '22 at 10:59
13

You should compare the date components:

let date1 = NSDate(timeIntervalSinceNow: 0)
let date2 = NSDate(timeIntervalSinceNow: 3600)

let components1 = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: date1)
let components2 = NSCalendar.currentCalendar().components([.Year, .Month, .Day], fromDate: date2)

if components1.year == components2.year && components1.month == components2.month && components1.day == components2.day {
    print("same date")
} else {
    print("different date")
}

Or shorter:

let diff = Calendar.current.dateComponents([.day], from: self, to: date)
if diff.day == 0 {
    print("same day")
} else {
    print("different day")
}
Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
Mike Henderson
  • 2,028
  • 1
  • 13
  • 32