如何在swift 3中添加日期到日期

时间:2022-08-25 18:25:39

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/08/15 and end date is 2017/08/16 and number of days is 2.

我从服务中获取天数,开始日期和结束日期。我想获得开始日期和结束日期之间所有日期的列表。假设我的开始日期是2017/08/15,结束日期是2017/08/16,天数是2。

But I am getting the date list like this.

但我得到这样的日期列表。

##### CONVERTED STRING DATE 2017-08-16

##### CONVERTED STRING DATE  2017-08-17

##### CONVERTED STRING DATE 2017-08-18

And I have another date like this

我有另一个这样的日期

Start date 2017/08/23 end date 2017/09/01 and number of days 8. then I get the list like this.

开始日期2017/08/23结束日期2017/09/01和天数8.然后我得到这样的列表。

##### CONVERTED STRING DATE  2017-08-24

##### CONVERTED STRING DATE  2017-08-25

##### CONVERTED STRING DATE  2017-08-28

##### CONVERTED STRING DATE  2017-08-29

##### CONVERTED STRING DATE  2017-08-30

##### CONVERTED STRING DATE  2017-08-31


##### CONVERTED STRING DATE  2017-09-01

This is how I get the dates array

这就是我获取日期数组的方法

 numberOfDates=Int(ceil(numOfDay))
                        //numberOfDates=numberOfDates-1
                        let arrayDates=self.generateDates(startDate: startDate, addbyUnit: .day, value: numberOfDates)

This is how my date calculation method

这是我的日期计算方法

internal func generateDates(startDate :Date?, addbyUnit:Calendar.Component, value : Int) -> [Date]
{
    //print("####START DATE#######\(startDate)")
    var calendar = Calendar.current
    calendar.timeZone=TimeZone.current
    var datesArray: [Date] =  [Date] ()

    for i in 0 ... value {
        var addAmount:Int!
        if(value==0)
        {
            addAmount=0
        }
        else
        {
            addAmount=1
        }
        if let newDate = calendar.date(byAdding: addbyUnit, value: i + addAmount, to: startDate!) {

            let strDayName=self.getDayName(mydate: newDate)
            if (strDayName != "Saturday" && strDayName != "Sunday")
            {
                datesArray.append(newDate)
            }

        }
    }

    return datesArray
}

My problem is sometimes the date list is wrong (1st scenario) but its correct in the 2nd scenario.

我的问题有时是日期列表错误(第一种情况),但在第二种情况下它是正确的。

2 个解决方案

#1


4  

A simple while loop will get you what you need. Example:

一个简单的while循环将为您提供所需的一切。例:

func generateDates(startDate :Date?, addbyUnit:Calendar.Component, value : Int) -> [Date] {

    var dates = [Date]()
    var date = startDate!
    let endDate = Calendar.current.date(byAdding: addbyUnit, value: value, to: date)!
    while date < endDate {
        date = Calendar.current.date(byAdding: addbyUnit, value: 1, to: date)!
        dates.append(date)
    }
    return dates
}

Edit: Or you can change your implementation slightly if you get your end date in advance

编辑:或者如果您提前结束日期,则可以稍微更改您的实施

func generateDates(between startDate: Date?, and endDate: Date?, byAdding: Calendar.Component) -> [Date] {

    var dates = [Date]()
    guard var date = startDate, let endDate = endDate else {
        return []
    }
    while date < endDate {
        date = Calendar.current.date(byAdding: byAdding, value: 1, to: date)!
        dates.append(date)
    }
    return dates
}

#2


6  

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.

你使它变得非常复杂。只需使用简单的日期类方法来区别,并使用for循环和Calendar类生成新日期。

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)
}

#1


4  

A simple while loop will get you what you need. Example:

一个简单的while循环将为您提供所需的一切。例:

func generateDates(startDate :Date?, addbyUnit:Calendar.Component, value : Int) -> [Date] {

    var dates = [Date]()
    var date = startDate!
    let endDate = Calendar.current.date(byAdding: addbyUnit, value: value, to: date)!
    while date < endDate {
        date = Calendar.current.date(byAdding: addbyUnit, value: 1, to: date)!
        dates.append(date)
    }
    return dates
}

Edit: Or you can change your implementation slightly if you get your end date in advance

编辑:或者如果您提前结束日期,则可以稍微更改您的实施

func generateDates(between startDate: Date?, and endDate: Date?, byAdding: Calendar.Component) -> [Date] {

    var dates = [Date]()
    guard var date = startDate, let endDate = endDate else {
        return []
    }
    while date < endDate {
        date = Calendar.current.date(byAdding: byAdding, value: 1, to: date)!
        dates.append(date)
    }
    return dates
}

#2


6  

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.

你使它变得非常复杂。只需使用简单的日期类方法来区别,并使用for循环和Calendar类生成新日期。

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)
}