I have Array ;
var mydates : [String] = []
let startDate = "2018-03-01"
let endDate = "2018-03-03"
And I have 3 variable , startDate
, endDate
, dates
, i want to append that variables, like;
if startDate
= 2018-03-01, and endDate
= 2018-03-03
will be add dates variable inside = "2018-03-01,2018-03-02,2018-03-03" between all dates from start and end dates.
How can i do it in swift 2 any idea ?
Here is Solution for Print all dates between two Date (Swift 4 Code)
var mydates : [String] = []
let startDate = "2018-03-01"
let endDate = "2018-03-05"
var dateFrom = Date() // First date
var dateTo = Date() // Last date
// Formatter for printing the date, adjust it according to your needs:
let fmt = DateFormatter()
fmt.dateFormat = "yyy-MM-dd"
dateFrom = fmt.date(from: startDate)!
dateTo = fmt.date(from: endDate)!
while dateFrom <= dateTo {
mydates.append(fmt.string(from: dateFrom))
dateFrom = Calendar.current.date(byAdding: .day, value: 1, to: dateFrom)!
}
print(mydates) // Your Result
Output is:
["2018-03-01", "2018-03-02", "2018-03-03", "2018-03-04", "2018-03-05"]