Swift: stampa tutte le date tra due NSDate ()

Sto creando una data usando NSDateComponents() :

 let startDate = NSDateComponents() startDate.year = 2015 startDate.month = 9 startDate.day = 1 let calendar = NSCalendar.currentCalendar() let startDateNSDate = calendar.dateFromComponents(startDate)! 

… ora voglio stampare tutte le date dalla data di NSDate() fino ad oggi, NSDate() . Ho già provato a giocare con NSCalendarUnit, ma mostra solo la differenza completa, non le singole date tra.

 let unit: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second] let diff = NSCalendar.currentCalendar().components(unit, fromDate: startDateNSDate, toDate: NSDate(), options: []) 

Come posso stampare tutte le date tra due NSDate?

Basta aggiungere un’unità di un giorno alla data fino a quando non raggiunge la data corrente (codice Swift 2):

 var date = startDateNSDate // first date let endDate = NSDate() // last date // Formatter for printing the date, adjust it according to your needs: let fmt = NSDateFormatter() fmt.dateFormat = "dd/MM/yyyy" // While date < = endDate ... while date.compare(endDate) != .OrderedDescending { print(fmt.stringFromDate(date)) // Advance by one day: date = calendar.dateByAddingUnit(.Day, value: 1, toDate: date, options: [])! } 

Aggiornamento per Swift 3:

 var date = startDate // first date let endDate = Date() // last date // Formatter for printing the date, adjust it according to your needs: let fmt = DateFormatter() fmt.dateFormat = "dd/MM/yyyy" while date < = endDate { print(fmt.string(from: date)) date = calendar.date(byAdding: .day, value: 1, to: date)! } 

Utilizzando l’estensione:

 extension Date{ func generateDatesArrayBetweenTwoDates(startDate: Date , endDate:Date) ->[Date] { var datesArray: [Date] = [Date]() var startDate = startDate let calendar = Calendar.current let fmt = DateFormatter() fmt.dateFormat = "yyyy-MM-dd" while startDate < = endDate { datesArray.append(startDate) startDate = calendar.date(byAdding: .day, value: 1, to: startDate)! } return datesArray } } 

Uso:

 let datesBetweenArray = Date().generateDatesArrayBetweenTwoDates(startDate: Your Start Date Object , endDate: Your End Date Object) 

Stessa cosa, ma più carina:

 extension Date { func allDates(till endDate: Date) -> [Date] { var date = self var array: [Date] = [] while date < = endDate { array.append(date) date = Calendar.current.date(byAdding: .day, value: 1, to: date)! } return array } } 

Come ottenere tutte le date per i prossimi 20 giorni:

 if let date = Calendar.current.date(byAdding: .day, value: 20, to: Date()) { print(Date().allDates(till: date)) } 

Sto usando questo approccio (Swift 3):

 import Foundation class Dates { static func printDatesBetweenInterval(_ startDate: Date, _ endDate: Date) { var startDate = startDate let calendar = Calendar.current let fmt = DateFormatter() fmt.dateFormat = "yyyy-MM-dd" while startDate < = endDate { print(fmt.string(from: startDate)) startDate = calendar.date(byAdding: .day, value: 1, to: startDate)! } } static func dateFromString(_ dateString: String) -> Date { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" return dateFormatter.date(from: dateString)! } } 

e lo chiamo così:

 Dates.printDatesBetweenInterval(Dates.dateFromString("2017-01-02"), Dates.dateFromString("2017-01-9")) 

L’output è:

 2017-01-02 2017-01-03 2017-01-04 2017-01-05 2017-01-06 2017-01-07 2017-01-08 2017-01-09 

Il tuo codice desiderato diventa simile

  let startDate = NSDateComponents() startDate.year = 2015 startDate.month = 9 startDate.day = 1 let calendar = NSCalendar.currentCalendar() let startDateNSDate = calendar.dateFromComponents(startDate)! var offsetComponents:NSDateComponents = NSDateComponents(); offsetComponents.day = 1 var nd:NSDate = startDateNSDate; println(nd) while nd.timeIntervalSince1970 < NSDate().timeIntervalSince1970 { nd = calendar.dateByAddingComponents(offsetComponents, toDate: nd, options: nil)!; println(nd) } 

Ecco la soluzione di stampare tutte le date tra due date (codice Swift 4)

 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 

L'output è:

 ["2018-03-01", "2018-03-02", "2018-03-03", "2018-03-04", "2018-03-05"]