Calculating time difference in minutes between two dates in Swift

In some cases, we need to get time difference in Swift for two dates. In this post I will show a quick example to get the difference in minutes, we can use the built-in timeIntervalSince. Say I have two Dates like:

var startTime:Date?
var endTime:Date?
//And some codes to assign the values to startTime to endTime. Let's assume startTime is earlier and endTime is later.

We can easily get the time difference from startTime to endTime by:

var diff = endTime.timeIntervalSince(startTime)

This is quite straightforward. However the diff here is in seconds. So to get minutes we can do:

var diff = endTime.timeIntervalSince(startTime) / 60

Leave a ReplyCancel reply