Skip to content

Instantly share code, notes, and snippets.

@martincalvert
Created August 22, 2017 18:26
Show Gist options
  • Save martincalvert/2713bd35fd355e0773c60961950a8b27 to your computer and use it in GitHub Desktop.
Save martincalvert/2713bd35fd355e0773c60961950a8b27 to your computer and use it in GitHub Desktop.
Swift Difference Between Dates
//: Playground - noun: a place where people can play
import Foundation
let now = Date()
let future = Date(timeIntervalSinceNow: TimeInterval(1000000))
let currentCalendar = Calendar.current
let calendarScale: Set<Calendar.Component> = [.day, .hour, .minute]
let difference = currentCalendar.dateComponents(calendarScale, from: now, to: future)
print("Days: \(difference.day!) Hours: \(difference.hour!) Seconds: \(difference.minute!)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment