简体   繁体   中英

Compare two dates in Swift

So I have to save my cache with the timestamp(named timestamp1) The next time app loads, I have to compare the difference with the current timestamp and timestamp1.

How will I implement this?

You can directly access it through property timeIntervalSinceNow of Date . Also, there is no need to use NSDate in Swift 3. Use Date and date(from:) method of DateFormatter return instance of Date? not NSDate? .

let timeInterval = date.timeIntervalSinceNow

And you can get TimeInterval between two dates using method timeIntervalSince(_:) .

let timeInterval = date1.timeIntervalSince(date2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM