0

I want to enable a feature to the user to execute a functionality only AFTER 1 hour from current time. What exactly shall I pass in toDate parameter in below code to get the 1 hour future time difference from current time? I have no idea how to set future time in NSDateFormatter OR NSDateComponents. So couldn't write what I have tried.

- (NSInteger)timeDifference:(NSDate *)fromDate ToDate:(NSDate *)toDate
{

    NSLog(@" FromDate: %@, ToDate: %@",fromDate,toDate);

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *differenceValue = [calendar components:NSHourCalendarUnit
                                                    fromDate:fromDate toDate:toDate options:0];
    NSLog(@"Calculated hour difference : %d",[differenceValue hour]);
    NSInteger hourDifference = [differenceValue hour];

    NSLog(@" HourDifference: %d",hourDifference);
    return hourDifference;
}
0

2 Answers 2

3

If you want to create a date that is one hour later the a specific date, you can use the dateByAddingTimeInterval method, like:

NSDate *date = [NSDate date];
NSDate *plusOneHour = [date dateByAddingTimeInterval:60.0f * 60.0f];

NSLog(@"%@, %@", date, plusOneHour);

UPDATED

As @uchuugaka stated, you can find the version using calendar methods here.

4
  • 1
    This is the naive and incorrect approach that will cause errors.
    – uchuugaka
    Apr 5, 2015 at 15:29
  • @uchuugaka I have never heard that this method not works correctly, but you might be right. Apr 5, 2015 at 15:36
  • It will work much of time but NSDate is a thin wrapper around the time interval and doesn't know about the human part of calendars and timezones. In one sense it is correct. But in reality it misses all the crazy stuff that happens with this that are legislated arbitrary corner cases.
    – uchuugaka
    Apr 5, 2015 at 15:40
  • 1
    @uchuugaka you were right, I found an older answer, which has the correct calendar methods to solve this problem, and I updated my answer with a link. Apr 5, 2015 at 15:42
1

See the NSCalendar header for convenience methods. Also watch the WWDC video from 2013 on the topic of calendrical calculations. This stuff has been really slow to make it into the docs and simply adding a time interval is many times a mistake. All of the components are important to get this right. You need to have a calendar and a timezone to have a semantically meaningful hour increase.

It's a trap when dealing with midnight, leap year and daylight savings time which is different everywhere.

Specifically in this case I would recommend this from NSCalendar.h

/*
    This API returns a new NSDate object representing the date calculated by adding an amount of a specific component to a given date.
    The NSCalendarWrapComponents option specifies if the component should be incremented and wrap around to zero/one on overflow, and should not cause higher units to be incremented.
*/
- (NSDate *)dateByAddingUnit:(NSCalendarUnit)unit value:(NSInteger)value toDate:(NSDate *)date options:(NSCalendarOptions)options NS_AVAILABLE(10_9, 8_0);

But there are several methods in that header that would suffice. Please watch the WWDC video carefully a few times. Well worth your time and your users' experience will be better.

Not the answer you're looking for? Browse other questions tagged or ask your own question.