Say I have a date and I want to do two things:
- Reset the hour to a specified one.
- Increment the day by a certain amount.
By reading the documentation, it looks like you need to use two distinct steps:
// Step 1: Get today at 9 AM.
NSDateComponents *todayComponents = [[NSCalendar currentCalendar] components:(NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear ) fromDate:[NSDate date]];
[todayComponents setHour:9];
NSDate *todayAt9AM = [[NSCalendar currentCalendar] dateFromComponents:todayComponents];
// Step 2: Add one day to that date.
NSDateComponents *oneDay = [NSDateComponents new];
[oneDay setDay:1];
NSDateComponents *tomorrowAt9AM = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay toDate:todayAt9AM options:0];
However, it seems like there is a shortcut which involves only one step:
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear ) fromDate:[NSDate date]];
components.hour = 9;
components.day += 1;
NSDate *tomorrowAt9AM = [[NSCalendar currentCalendar] dateFromComponents:components];
This seems to work even when the date is 1/31/2014, wrapping around to 2/1/2014 at 9AM correctly.
The reason this seems strange is because you are sending dateFromComponents the following values:
- Year: 2014
- Month: 1
- Day: 32
- Hour: 9
- Minute: 34 (for example)
- Seconds: 12 (for example)
Notice how 32 is out of range in the Gregorian calendar.
Is it dangerous to rely on this shortcut (e.g. if it only happens to be working by chance)? Or is there any documentation for dateFromComponents that says it can be used in this way?
Also, how does it know when I want wrapping to occur vs. when I want to explicitly override one of these values?
Say I had set the month to 3, would it leave the month at 3, or does it first process the month I specified (3), and then process the days which could potentially increment month to 4? Is this undefined behavior, or is there some documented set of rules it follows?