Swift 对比两个 Date 之间相差天数

/// DateTools.swift

import Foundation

extension Date {
    func daysBetweenDate(toDate: Date) -> Int {
        let components = Calendar.current.dateComponents([.day], from: self, to: toDate) return components.day ?? 0 } } 
/// xxxController.swift

 let formatter = DateFormatter()
 formatter.dateFormat = "YYYYMMdd"
 let endDate = formatter.date(from: "20181220")

 /// 当前日期 - 考试日期
 let date = Date()
 let days = Date().daysBetweenDate(toDate: endDate!)
 print("相差天数为 ==", days)
相关文章
相关标签/搜索