Array for dates between two dates

I have two stellar date dates and the last date to write to the database (master data). Now I need to create a list of dates in the form of an array. Start date and end date, in lowercase form in the database.

date format is MM / dd / yyyy.

+5
source share
6 answers
// minDate and maxDate represent your date range
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *days = [[NSDateComponents alloc] init];
NSInteger dayCount = 0;
while ( TRUE ) {
    [days setDay: ++dayCount];
    NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: minDate options: 0];
    if ( [date compare: maxDate] == NSOrderedDescending )
        break;
    // Do something with date like add it to an array, etc.
}
[days release];
[gregorianCalendar release];
+11
source

I have a more general approach with NSCalendarUnitfor determining the step between dates and observing normalized dates.

API iOS 8, Swift 2.0

    func generateDates(calendarUnit: NSCalendarUnit, startDate: NSDate, endDate: NSDate) -> [NSDate] {

            let calendar = NSCalendar.currentCalendar()
            let normalizedStartDate = calendar.startOfDayForDate(startDate)
            let normalizedEndDate = calendar.startOfDayForDate(endDate)

            var dates = [normalizedStartDate]
            var currentDate = normalizedStartDate

            repeat {

                currentDate = calendar.dateByAddingUnit(calendarUnit, value: 1, toDate: currentDate, options: NSCalendarOptions.MatchNextTime)!
                dates.append(currentDate)

            } while !calendar.isDate(currentDate, inSameDayAsDate: normalizedEndDate)

            return dates
    }
+4
source

, ( ), NSDate.

In response to this question (below), I posted some methods that will provide the necessary transformations.

How to get datetime column in SQLite with Objective-C

+1
source

Here's a Costique solution in fast but with a little quick flash.

func ...(lhs:NSDate, rhs:NSDate) -> [NSDate] {
  var dates: [NSDate] = []
  var cal = NSCalendar.currentCalendar() // or NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
  var days = NSDateComponents()
  var dayCount = 0
  while true {
    days.day = dayCount
    let date:NSDate = cal.dateByAddingComponents(days, toDate: lhs, options: NSCalendarOptions.allZeros)!
    if date.compare(rhs) == .OrderedDescending {
      break
    }
    dayCount += 1
    dates.append(date)
  }

  return dates
}

let fromDate = NSDate(timeIntervalSince1970: 1440711319)
let toDate = NSDate(timeIntervalSince1970: 1441316129)

fromDate...toDate // => ["Aug 27, 2015, 2:19 PM", "Aug 28, 2015, 2:19 PM", "Aug 29, 2015, 2:19 PM", "Aug 30, 2015, 2:19 PM", "Aug 31, 2015, 2:19 PM", "Sep 1, 2015, 2:19 PM", "Sep 2, 2015, 2:19 PM", "Sep 3, 2015, 2:19 PM"]
+1
source

Swift 3.0:

Suppose you want an array of dates from today to the next 60 days.

extension Date {
    func generateDates(startDate :Date?, addbyUnit:Calendar.Component, value : Int) -> [Date]
{
    let calendar = Calendar.current
    var datesArray: [Date] =  [Date] ()

    for i in 0 ... value {
        if let newDate = calendar.date(byAdding: addbyUnit, value: i + 1, to: startDate!) {
            datesArray.append(newDate)
        }
    }

    return datesArray
}
}

Application:

var datesArrayByAddingDays:[Date]?

override func viewWillAppear(_ animated: Bool) {

    datesArrayByAddingDays = Date().generateDates(startDate: Date(), addbyUnit: .day, value: 60)
}
0
source

Swift4 @Nick Wargnier's version:

func ...(lhs:Date, rhs:Date) -> [Date] {
    var dates = [Date]()
    let cal = NSCalendar.current
    var days = DateComponents()
    var dayCount = 0
    while true {
        days.day = dayCount
        let date = cal.date(byAdding: days, to: lhs)!
        if date.compare(rhs) == .orderedDescending {
            break
        }
        dayCount += 1
        dates.append(date)
    }

    return dates
}
0
source

All Articles