Swift Time Difference Between Date Objects in Seconds

black marker on notebook

To calculate the time difference between two Date objects in seconds in Swift, use the Date.timeIntervalSinceReferenceDate method.

For example, let’s calculate how many seconds is between Christmas and New Year:

import Foundation

let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"

let xmas = formatter.date(from: "2021/12/24 00:00")
let newYear = formatter.date(from: "2022/01/01 00:00")

let diffSeconds = newYear!.timeIntervalSinceReferenceDate - xmas!.timeIntervalSinceReferenceDate

print(diffSeconds)

Output:

691200.0

Date Difference Extension in Swift

If you need to compute time differences between two dates more often in your project, feel free to write an extension for the Date class. This particular extension makes it possible to subtract Date objects from one another:

import Foundation

extension Date {
    static func - (lhs: Date, rhs: Date) -> TimeInterval {
        return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
    }
}

Now you can use it to get the time difference between two date objects:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"

let xmas = formatter.date(from: "2021/12/24 00:00")
let newYear = formatter.date(from: "2022/01/01 00:00")

print(newYear! - xmas!)

Output:

691200.0

This is the number of seconds between the two dates.

Date Difference in Minutes, Hours, Days, or Years in Swift

Now you know how to get the time difference between two objects in seconds. From this information you can easily calculate the difference in minutes, hours, and so on.

I will show you the formulas:

To convert seconds to minutes in Swift:

minutes = seconds / 60.0

To convert seconds to hours in Swift:

hours = seconds / (60.0 * 60.0)

To convert seconds to days in Swift:

days = seconds / (60.0 * 60.0 * 24.0)

To convert seconds to weeks in Swift:

weeks = seconds / (60.0 * 60.0 * 24.0 * 7.0)

To convert seconds to months in Swift you need to notice not every month is equal in length. To account for this, let’s use the average number of days in a month, that is, 30.4369:

months = seconds / (60.0 * 60.0 * 24.0 * 30.4369)

To convert seconds to years in Swift you need to remember that a year is not exactly 365 days in length. On average it is 365.2422 days long:

years = seconds / (60.0 * 60.0 * 24.0 * 365.2422)

Now that you know how to convert seconds to other common units in time, you can implement these as an extension to TimeInterval, which is a Double that represents seconds:

extension TimeInterval {
    func asMinutes() -> Double { return self / (60.0) }
    func asHours()   -> Double { return self / (60.0 * 60.0) }
    func asDays()    -> Double { return self / (60.0 * 60.0 * 24.0) }
    func asWeeks()   -> Double { return self / (60.0 * 60.0 * 24.0 * 7.0) }
    func asMonths()  -> Double { return self / (60.0 * 60.0 * 24.0 * 30.4369) }
    func asYears()   -> Double { return self / (60.0 * 60.0 * 24.0 * 365.2422) }
}

Now you can use this extension in conjunction with the code you wrote in the previous example.

For example, let’s calculate the time difference between Christmas 1969 and New Year 2000:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"

let xmas1969 = formatter.date(from: "1969/12/24 00:00")
let newYear2000 = formatter.date(from: "2000/01/01 00:00")

// Difference in seconds
let difference = (newYear2000! - xmas1969!)

print(difference, "seconds")
print(difference.asMinutes(), "minutes")
print(difference.asHours(), "hours")
print(difference.asDays(), "days")
print(difference.asWeeks(), "weeks")
print(difference.asMonths(), "months")
print(difference.asYears(), "years")

Output:

947376000.0 seconds
15789600.0 minutes
263160.0 hours
10965.0 days
1566.4285714285713 weeks
360.2535080773666 months
30.021174990184594 years

Conclusion

Today you learned how to calculate the time difference between two Date objects in Swift.

For your convenience, here is all the code we wrote in this guide. This code writes an extension to the Date class to subtract dates from one another to get the difference between them in seconds. It also extends the TimeInterval to convert seconds to minutes, hours, days, weeks, months, and years.

import Foundation

extension Date {
    static func - (lhs: Date, rhs: Date) -> TimeInterval {
        return lhs.timeIntervalSinceReferenceDate - rhs.timeIntervalSinceReferenceDate
    }
}

extension TimeInterval {
    func asMinutes() -> Double { return self / (60.0) }
    func asHours()   -> Double { return self / (60.0 * 60.0) }
    func asDays()    -> Double { return self / (60.0 * 60.0 * 24.0) }
    func asWeeks()   -> Double { return self / (60.0 * 60.0 * 24.0 * 7.0) }
    func asMonths()  -> Double { return self / (60.0 * 60.0 * 24.0 * 30.4369) }
    func asYears()   -> Double { return self / (60.0 * 60.0 * 24.0 * 365.2422) }
}

let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"

let xmas1969 = formatter.date(from: "1969/12/24 00:00")
let newYear2000 = formatter.date(from: "2000/01/01 00:00")

let difference = (newYear2000! - xmas1969!)

print(difference, "seconds")
print(difference.asMinutes(), "minutes")
print(difference.asHours(), "hours")
print(difference.asDays(), "days")
print(difference.asWeeks(), "weeks")
print(difference.asMonths(), "months")
print(difference.asYears(), "years")

Thanks for reading. Happy coding!

Further Reading

50 Swift Interview Questions

iOS Development Tips

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.