Date Comparison

suggest change

There are 4 methods for comparing dates:

Swift

These names are Swift 2. In Swift 3 and later, equality is isEqual(to:), and the comparison results are .orderedAscending, .orderedSame, and .orderedDescending.

Objective-C

Let’s say we have 2 dates:

Swift

let date1: NSDate = ... // initialized as  July 7, 2016 00:00:00
let date2: NSDate = ... // initialized as  July 2, 2016 00:00:00

Objective-C

NSDate *date1 = ... // initialized as  July 7, 2016 00:00:00
NSDate *date2 = ... // initialized as  July 2, 2016 00:00:00

Then, to compare them, we try this code:

Swift

if date1.isEqualToDate(date2) {
    // returns false, as both dates aren't equal
}

let earlierDate: NSDate = date1.earlierDate(date2) // returns the earlier date of the two (date2)
let laterDate: NSDate = date1.laterDate(date2) // returns the later date of the two (date1)

let result: NSComparisonResult = date1.compare(date2)

if result == .OrderedAscending {
    // true if date1 is earlier than date2
} else if result == .OrderedSame {
    // true if the dates are the same
} else if result == .OrderedDescending {
    // true if date1 is later than date2
}

Objective-C

if ([date1 isEqualToDate:date2]) {
    // returns false, because the dates are not equal
}

NSDate *earlierDate = [date1 earlierDate:date2]; // returns the earlier of the two dates, here date2
NSDate *laterDate = [date1 laterDate:date2]; // returns the later of the two dates, here date1

NSComparisonResult result = [date1 compare:date2];
if (result == NSOrderedAscending) {
    // fails
    // comes here if date1 is earlier than date2, in our case it will not come here
} else if (result == NSOrderedSame){
    // fails
    // comes here if date1 is same as date2, in our case it will not come here
} else{ // NSOrderedDescending
    // succeeds
    // comes here if date1 is later than date2, in our case it will come here
}

To describe elapsed time, or to read calendar components:

Swift 3

let dateStringUTC = "2016-10-22 12:37:48 +0000"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let date = dateFormatter.date(from: dateStringUTC)!

let now = Date()

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.maximumUnitCount = 2
let string = formatter.string(from: date, to: now)! + " " + NSLocalizedString("ago", comment: "added after elapsed time to say how long before")

Or you can use this for each component:

// get the current date and time
let currentDateTime = Date()

// get the user's calendar
let userCalendar = Calendar.current

// choose which date and time components are needed
let requestedComponents: Set<Calendar.Component> = [
    .year,
    .month,
    .day,
    .hour,
    .minute,
    .second
]

// get the components
let dateTimeComponents = userCalendar.dateComponents(requestedComponents, from: currentDateTime)

// Each value is optional. It is nil when that component was not requested.
let year = dateTimeComponents.year
let month = dateTimeComponents.month
let day = dateTimeComponents.day
let hour = dateTimeComponents.hour
let minute = dateTimeComponents.minute
let second = dateTimeComponents.second

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents