Difference between two NSDate objects -- Result also a NSDate

49,250

Solution 1

NSDate represents an instance in time, so it doesn't make sense to represent an interval of time as an NSDate. What you want is NSDateComponents:

NSDate *dateA;
NSDate *dateB;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
                                           fromDate:dateA
                                             toDate:dateB
                                            options:0];

NSLog(@"Difference in date components: %i/%i/%i", components.day, components.month, components.year);

Solution 2

If you subtract 12/12/2001 from 05/05/2002 what will be the date? The chronological distance between two dates can't be a date, it's alway some kind of interval. You can use timeIntervalSinceDate: to calculate the interval.

To localize you can try the following steps:

Solution 3

From NSDate class reference, you have instance methods to do these -

  1. How to compare two NSDate variables? Ans: isEqualToDate:
  2. How to find difference between two NSDate variables? Ans: timeIntervalSinceDate:
  3. How to get each separate value of minute, hours and days from NSDate variable? links

Solution 4

You can calculate the time interval between two dates using NSDate's timeIntervalSinceDate:, but it doesn't make any sense for you to represent a time interval as a date.

Solution 5

There is a easy way by using -compare: in NSDate:

NSDate *dateA = [NSDate dateWithTimeIntervalSinceNow:100];
NSDate *dateB = [NSDate dateWithTimeIntervalSinceNow:200];
NSDate *myDate = [NSDate dateWithTimeIntervalSinceNow:150];
NSArray *dateArray = [NSArray arrayWithObjects:dateA, dateB, myDate, nil];
NSArray *sortedArray = [dateArray sortedArrayUsingSelector:@selector(compare:)];
if ([myDate isEqualToDate:[sortedArray objectAtIndex:1]])
    NSLog(@"myDatea between dateA and dateB");
Share:
49,250
Abhinav
Author by

Abhinav

Updated on July 09, 2022

Comments

  • Abhinav
    Abhinav almost 2 years

    I have two NSDate objects and I want the difference between the two and the result should again be a NSDate object. Any idea how to achieve this?

    Here, I am trying to address a unique problem where I have to find out the elapsed time and then localize the elapsed time. I can localize it if I have the elapsed time in NSDate object. So thought of creating a NSDate object which has its time component same as time interval between the two dates so that I could use NSDateFormatter to localize it.

    • Damien_The_Unbeliever
      Damien_The_Unbeliever about 13 years
      The difference between any two arbitrarily selected dates is always 19th February 1986. :-)
    • cfischer
      cfischer almost 13 years
      Sorry, but I don't get it. :-P
  • Abhinav
    Abhinav about 13 years
    I understand this. But I am trying to address a unique problem where I have to find out the elapsed time and then localize the elapsed time. I can localize it if I have the elapsed time in NSDate object. So thought of creating a NSDate object which has its time component same as time interval between the two dates so that I could use NSDateFormatter to localize it.
  • Nick Weaver
    Nick Weaver about 13 years
    @Abhinav I understand, there is a second problem arising: What is a month? Some months have 31 days others have 30. To represent an interval with the term month is very precise. You have to take that into account.
  • Aaron Ash
    Aaron Ash over 7 years
    As of iOS 8, the calendar components are now (for this example): NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
  • Nick Forge
    Nick Forge over 7 years
    Thanks @AaronAsh, I've updated the answer to reflect the latest constant names.
  • isaiasmac
    isaiasmac over 7 years
    In iOS 8 NSGregorianCalendar is deprecated. Replace with NSCalendarIdentifierGregorian