如何在Swift 3中为日期添加天数 [英] How to add days to a date in swift 3

查看:406
本文介绍了如何在Swift 3中为日期添加天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是我得到的日期列表像

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

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

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

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

这是我的日期计算方法

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.

推荐答案

一个简单的while循环将得到您所需要的。
示例:

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

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
}

编辑:或者,如果提前提前结束日期,也可以稍作更改

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
}

这篇关于如何在Swift 3中为日期添加天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆