Comparing NSDates without time component

后端 未结 17 1610
轮回少年
轮回少年 2020-11-27 12:49

In a swift playground, I have been using

NSDate.date() 

But, this always appears with the time element appended. For my app I need to ignor

相关标签:
17条回答
  • 2020-11-27 13:01

    For Swift3

    var order = NSCalendar.current.compare(firstDate, to: secondDate, toGranularity: .hour)
    
    if order == .orderedSame {
        //Both the dates are same. 
        //Your Logic.
    }
    
    0 讨论(0)
  • 2020-11-27 13:04

    In my experience, most people's problems with using NSDate comes from the incorrect assumption that an NSDate can be used to represent a date in the 'normal' sense (i.e. a 24 period starting at midnight in the local timezone). In normal (everyday / non-programming) usage, 1st January 2014 in London is the same date as 1st January in Beijing or New York even though they cover different periods in real time. To take this to the extreme, the time on Christmas Island is UTC+14 while the time on Midway Island is UTC-11. So 1st January 2014 on these two island are the same date even though one doesn't even start until the other has been completed for an hour.

    If that is the kind of date you are recording (and if you are not recording the time component, it probably is), then do not use NSDate (which stores only seconds past 2001-01-01 00:00 UTC, nothing else) but store the year month and day as integers - perhaps by creating your own CivilDate class that wraps these values - and use that instead.

    Only dip into NSDate to compare dates and then make sure to explicitly declare the time zone as "UTC" on both NSDates for comparison purposes.

    0 讨论(0)
  • 2020-11-27 13:06

    I wrote the following method to compare two dates by borrowing from Ashley Mills solution. It compares two dates and returns true if the two dates are the same (stripped of time).

    func compareDate(date1:NSDate, date2:NSDate) -> Bool {
        let order = NSCalendar.currentCalendar().compareDate(date1, toDate: date2,
            toUnitGranularity: .Day)
        switch order {
        case .OrderedSame:
            return true
        default:
            return false
        }
    }
    

    And it is called like this:

    if compareDate(today, date2: anotherDate) {
        // The two dates are on the same day.
    }
    
    0 讨论(0)
  • 2020-11-27 13:07

    To answer your question:

    Is this possible in Swift?

    Yes, it is possible


    Ahh, you also want to now HOW

    let cal = NSCalendar.currentCalendar()
    cal.rangeOfUnit(.DayCalendarUnit, startDate: &d1, interval: nil, forDate: d1) // d1 NSDate?
    cal.rangeOfUnit(.DayCalendarUnit, startDate: &d2, interval: nil, forDate: d2) // d2 NSDate?
    

    Now d1 and d2 will contain the dates at beginning of their days.

    compare with d1!.compare(d2!)

    To display them without time portion, us NSDateFormatter.

    0 讨论(0)
  • 2020-11-27 13:08

    Xcode 11.2.1, Swift 5 & Above

    Checks whether the date has same day component.

    Calendar.current.isDate(date1, equalTo: date2, toGranularity: .day)
    

    Adjust toGranularity as your need.

    0 讨论(0)
  • 2020-11-27 13:09

    Swift 3

            let order = NSCalendar.current.compare(date1, to: date2, toGranularity: .day)
    
            if order == .orderedAscending { 
              // date 1 is older
            }
            else if order == .orderedDescending { 
              // date 1 is newer
            }
            else if order == .orderedSame { 
              // same day/hour depending on granularity parameter
            }
    
    0 讨论(0)
提交回复
热议问题