Swift 3 - find number of calendar days between two dates

前端 未结 11 2044
别跟我提以往
别跟我提以往 2020-12-12 23:51

The way I did this in Swift 2.3 was:

let currentDate         = NSDate()
let currentCalendar     = NSCalendar.currentCalendar()

var startDate : NSDate?
var e         


        
11条回答
  •  不思量自难忘°
    2020-12-13 00:22

    In Swift 4 there is a simple one-liner to get the number of days (or any other DateComponent) between two dates:

    let diffInDays = Calendar.current.dateComponents([.day], from: dateA, to: dateB).day
    

提交回复
热议问题