Skip to main content
added 1998 characters in body
Source Link
Krunal
  • 79.9k
  • 49
  • 253
  • 268

Swift 4
Try this and see (date range with String):

// Start & End date string let startingAt = "01/01/2018" let endingAt = "08/03/2018" // Sample date formatter let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd/MM/yyyy" // start and end date object from string dates var startDate = dateFormatter.date(from: startingAt) ?? Date() let endDate = dateFormatter.date(from: endingAt) ?? Date() // Actual operational logic var dateRange: [String] = [] while startDate <= endDate { let stringDate = dateFormatter.string(from: startDate) startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date() dateRange.append(stringDate) } print("Resulting Array - \(dateRange)") 

Swift 3

var date1 = Date(string: "2010-01-01 00:00:00 +0000") var date2 = Date(string: "2010-02-03 00:00:00 +0000") var secondsBetween: TimeInterval = date2.timeIntervalSince(date1) var numberOfDays: Int = secondsBetween / 86400 print(numberOfDays) 

Swift 3

var date1 = Date(string: "2010-01-01 00:00:00 +0000") var date2 = Date(string: "2010-02-03 00:00:00 +0000") var secondsBetween: TimeInterval = date2.timeIntervalSince(date1) var numberOfDays: Int = secondsBetween / 86400 print(numberOfDays) 

Swift 4
Try this and see (date range with String):

// Start & End date string let startingAt = "01/01/2018" let endingAt = "08/03/2018" // Sample date formatter let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd/MM/yyyy" // start and end date object from string dates var startDate = dateFormatter.date(from: startingAt) ?? Date() let endDate = dateFormatter.date(from: endingAt) ?? Date() // Actual operational logic var dateRange: [String] = [] while startDate <= endDate { let stringDate = dateFormatter.string(from: startDate) startDate = Calendar.current.date(byAdding: .day, value: 1, to: startDate) ?? Date() dateRange.append(stringDate) } print("Resulting Array - \(dateRange)") 

Swift 3

var date1 = Date(string: "2010-01-01 00:00:00 +0000") var date2 = Date(string: "2010-02-03 00:00:00 +0000") var secondsBetween: TimeInterval = date2.timeIntervalSince(date1) var numberOfDays: Int = secondsBetween / 86400 print(numberOfDays) 
Source Link
Krunal
  • 79.9k
  • 49
  • 253
  • 268

Swift 3

var date1 = Date(string: "2010-01-01 00:00:00 +0000") var date2 = Date(string: "2010-02-03 00:00:00 +0000") var secondsBetween: TimeInterval = date2.timeIntervalSince(date1) var numberOfDays: Int = secondsBetween / 86400 print(numberOfDays)