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.
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.
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);
// 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];
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
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With