How to add days to a date in swift 3

后端 未结 2 1916
南笙
南笙 2021-01-17 10:23

I am getting number of days,Start date and end date from a service. I want to get the list of all dates in between start date and end date. Let\'s say my start date is 2017/

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-17 10:36

    You are making it really complicated.Just use simple date class methods for difference and generate new dates with a for loop and Calendar class.

    let startDate = Date()
    let endDate = Date(timeInterval: 2*86400, since: startDate)
    
    
    let components = Calendar.current.dateComponents([.day], from: startDate, to: endDate)
    let numberOfDays = components.day ?? 0
    
    for i in 1...numberOfDays {
        let nextDate = Calendar.current.date(byAdding: .day, value: i, to: startDate)
        print(nextDate)
    }
    

提交回复
热议问题