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