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

前端之家收集整理的这篇文章主要介绍了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)
原文链接:https://www.f2er.com/swift/320478.html

猜你在找的Swift相关文章