How can I create a complete NSDate object that represents the next given day?

I probably didn’t make this heading clear enough, but if the user indicates the time at 14:30 and it is now 2:00 pm, I need an NSDate object that represents the current day with the time of 14:30. If the user indicates the time at 14:30 and currently 3:00 pm, I need an NSDate object, which is tomorrow at 14:30. Similar to how an alarm clock works.

I already wrote this code, but it's embarrassingly long, and it feels really kludgy, and I feel that it should be simpler, but I'm new to iOS development and this particular API.

Thank you for your help!

+3
source share
4 answers

If you get userDate from user and nowDate = [NSDate date]then

NSComparisonResult diff = [userDate compare: nowDate];

If diff NSOrderedDescending, then userDate will be later than nowDate, and you will set the alarm for today. If it is NSOrderedAscending, then you will set an alarm for tomorrow. You can check NSOrderedSame, but it never will be.

You would not want to determine which day, so it seems to me that adding the NSTimeIntervaldifference is plus 24 * 60 * 60 (if the alarm is tomorrow) or just the difference (if it is the alarm today).

I do not know why everyone is trying to make alarms. This is impossible to do, as far as I can tell.

0
source

NSDate, /, ? , ( earlierDate: laterDate:) ( dateWithTimeInterval:sinceDate: initWithTimeInterval:sinceDate:).

- :

// Assume dateTarget is an NSDate representing the date/time the user picked.
// Is it earlier than now?
if( [dateTarget earlierDate:[NSDate date]] == dateTarget ) {
    // dateTarget is earlier than now.
    // Add 1 day to it.
    NSDate* newDate = [[NSDate alloc]initWithTimeInterval:60*60*24 sinceDate:dateTarget];
    [dateTarget release];
    dateTarget = newDate;
}

// dateTarget is now either the original time, or if that time passed, the same time tomorrow.
NSLog( @"dateTarget = %@", dateTarget );
+1

, , . , . , , , .

NSDateComponents:

// using while will be inefficient when the targetDate is more than a few days in the past
while ([targetDate compare:[NSDate date]] == NSOrderedAscending) {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger units = NSYearCalendarUnit |
                      NSMonthCalendarUnit |
                      NSDayCalendarUnit |
                      NSHourCalendarUnit |
                      NSMinuteCalendarUnit |
                      NSSecondCalendarUnit;
    NSDateComponents *comps = [calendar components:units fromDate:targetDate];
    [comps setDay:[comps day] + 1]; // if day is 32 for instance, it'll automatically roll over to the next month
    NSDate *targetDate = [calendar dateFromComponents:comps];
}

( targetDate, , ), , , 1 NSDate NSDateComponents.

+1

, UIDatePicker , :

NSDate * selectedDate = ...; //2:30pm
NSDate * selectedComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:selectedDate];

NSDate * now = [NSDate date];
NSDate * nowComponents = [[NSCalendar currentCalendar] components:NSUIntegerMax fromDate:now];

[nowComponents setHour:[selectedComponents hour]];
[nowComponents setMinute:[selectedComponents minute]];

NSDate * targetDate = [[NSCalendar currentCalendar] dateFromComponents:nowComponents];
if ([[now laterDate:targetDate] isEqual:now]) {
  //in a comparison between now and the target date, the target date has already passed
  [nowComponents setDay:[nowComponents day]+1];
  targetDate = [[NSCalendar currentCalendar] dateFromComponents:nowComponents];
}

//target date is now at the appropriate 2:30pm

: NSCalendarUnit NSUInteger, NSUIntegerMax . , OR.

+1

Source: https://habr.com/ru/post/1759058/


All Articles