前端之家收集整理的这篇文章主要介绍了
Swift 对比两个 Date 之间相差天数,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import Foundation
extension Date {
func daysBetweenDate(toDate: Date) -> Int {
let components = Calendar.current.dateComponents([.day],from: self,to: toDate) return components.day ?? 0 } }
let formatter = DateFormatter()
formatter.dateFormat = "YYYYMMdd"
let endDate = formatter.date(from: "20181220")
let date = Date()
let days = Date().daysBetweenDate(toDate: endDate!)
print("相差天数为 ==",days)
原文链接:https://www.f2er.com/swift/320478.html