0

I had Two Dates from date and to date. I stored two dates in NSString like string1 and string 2 respectively. Now My Problem is I want the difference of these two dates. My Date format is 04-Mar-2014 and 14-Mar-2014 and I want the result to be like 10.

Imran
  • 1,715
  • 2
  • 20
  • 42

5 Answers5

2

You can get difference by using the code given below:-

NSString *start = @"2010-09-01";
NSString *end = @"2010-12-01";

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
[f release];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
                                                    fromDate:startDate
                                                      toDate:endDate
                                                     options:0];
[gregorianCalendar release];

components now holds the difference.

NSLog(@"%ld", [components day]);

Courtesy:-vikingosegundo

https://stackoverflow.com/a/4576575/1865424

Community
  • 1
  • 1
Kundan
  • 3,084
  • 2
  • 28
  • 65
1

Three methods you need are:

+ (NSDateFormatter *)dateFormatter
{
    static NSDateFormatter *dateFormatter = nil;

    if (dateFormatter == nil)
    {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"YOUR_STRING_FORMAT"];
    }

    [NSTimeZone resetSystemTimeZone];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

    return dateFormatter;
}


+ (NSDate *)dateWithString:(NSString *)dateString
{
    return [[self dateFormatter] dateFromString:dateString];
}

- (NSInteger)distanceInDaysToDate:(NSDate *)date
{
    NSDateComponents *components1 = [CURRENT_CALENDAR components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self];
    components1.hour = 0;
    components1.minute = 0;
    components1.second = 0;

    NSDateComponents *components2 = [CURRENT_CALENDAR components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:date];
    components1.hour = 0;
    components1.minute = 0;
    components1.second = 0;

    NSDate *date1 = [CURRENT_CALENDAR dateFromComponents:components1];
    NSDate *date2 = [CURRENT_CALENDAR dateFromComponents:components2];

    NSDateComponents *components = [CURRENT_CALENDAR components:NSDayCalendarUnit fromDate:date1 toDate:date2 options:0];

    return components.day;
}

Best if you move them into NSDate category.

damirstuhec
  • 6,069
  • 1
  • 22
  • 39
1

You Two date is

NSString* str1 = @"04-Mar-2014";

NSString* str2 = @"14-Mar-2014";

Now convert two date in to DD-MM-YYY as below

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"dd-MM-yyyy"];

Now string convert into date

NSDate *date1 = [dateFormat dateFromString:str1];

NSDate *date2 = [dateFormat dateFromString:str2];

Calculation of convert between two date

NSTimeInterval distanceBetweenDates = [date2 timeIntervalSinceDate:date1];

double secondsInAnHour = 3600;

NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;

NSInteger day =  hoursBetweenDates/ 24;

Your OUTPUT : 10

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
0

While keeping the NSDate objects is the best solution, you can recreate NSDateobjects from the string using an NSDateFormatter with its format string set accordingly to your date format.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MMM-yy"];
[dateFormatter dateFromString:yourString];

Then you can use for example the timerIntervalSinceDate:method of NSDateand calculate the difference. Alternatively you can extract NSDateComponentsand get the difference that way.

Volker
  • 4,640
  • 1
  • 23
  • 31
0

I think, according to your question, my assumption is difference between two dates. You can use this.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *date1 = [dateFormat dateFromString:@"04-Mar-2014"];
NSDate *date2 = [dateFormat dateFromString:@"14-Mar-2014"];

NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

int numberOfDays = secondsBetween / 86400;

NSLog(@"There are %d days in between the two dates.", numberOfDays);
Mani
  • 17,549
  • 13
  • 79
  • 100
  • Thank you this is what the perfect answer is.. Thanks Dude.. Cheers!! – user3401290 Mar 14 '14 at 09:49
  • 3
    The solution privided by Damir179 or Popeye seems better since he used NSDateComponents. What if there is a change of hours (winter/summer)? You'll get a wrong number of days because you use 86400. – Larme Mar 14 '14 at 09:57
  • @Larme Can you pls clarify How is it difference with their answers? Give correct explanation with this. Why gave downvote? If you know correct answer, Just explain more. – Mani Mar 14 '14 at 10:17
  • Ok. In some regions, there are a change of hour in summer/winter, where at 2h00 in the morning, it will be 3h00 in the morning, (and reverse). So, days may have "23 or 25 hours". Dividing then by 86400 may cause a different result than expected. Plus NSDateComponents is the class designed by this. – Larme Mar 26 '14 at 19:03