Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array for dates between two dates

I am having two dates star date and last date for a entry in database(core data).Now i need to create a list of dates in an array form. Start date and end date having string form in data base.

format of date is MM/dd/yyyy.

like image 298
Ishu Avatar asked Feb 04 '11 08:02

Ishu


People also ask

How can I get a list of dates between two dates?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.

How do I get a list of dates between two dates in Python?

import pandas from datetime import datetime, timedelta startDate = datetime(2022, 6, 1) endDate = datetime(2022, 6, 10) # Getting List of Days using pandas datesRange = pandas. date_range(startDate,endDate-timedelta(days=1),freq='d') print(datesRange);


3 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];
like image 165
Costique Avatar answered Sep 28 '22 01:09

Costique


I have a more general approach with NSCalendarUnit for defining the step between the dates & taking care of the dates being normalized.

iOS 8 API, 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
    }
like image 39
Kex Avatar answered Sep 28 '22 00:09

Kex


You could accomplish this pretty easily by converting the start date to a Julian day (which will produce a float value), iterating through to the end date, and converting the iterated values from Julian days back into NSDate objects.

I posted some methods in my answer to this question (below) that will provide the necessary conversions.

How get a datetime column in SQLite with Objective C

like image 42
xyzzycoder Avatar answered Sep 28 '22 02:09

xyzzycoder