0

I am calculating difference between two dates in my testapp. It works fine if I don't change the month but when i change the month it doesn't work. Please help me here is code I am using:-

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
//[df setDateStyle:NSDateFormatterMediumStyle];
df.dateFormat = @"YYYY-MM-DD";

NSDate *date1 = [df dateFromString:@"2011-04-30"];
NSDate *date2 = [df dateFromString:@"2011-05-03"];

NSLog(@"date1=%@",date1);
NSLog(@"date2=%@",date2);
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

int numberOfDays = secondsBetween / 86400;

NSLog(@"There are %d days in between the two dates.", numberOfDays);

output is -27. I am confuse.

please anyone help me to fix this problem..

RAMAN RANA
  • 1,785
  • 4
  • 21
  • 40

4 Answers4

3

You can also try this method from which you can get any kind of difference from two date...

-(NSString *)timeSinceTimestamp:(NSString *)seconds
{
   double seconds2 = [seconds doubleValue];

   NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds2];

   NSDate *now = [NSDate date];

   double start = [date timeIntervalSince1970];

   double end = [now timeIntervalSince1970];

   double difference = (end - start) / 1000;

   difference = round(difference);

   int minutes = difference / 60;

   int hours = minutes / 60;

   int days = hours / 24;

   int weeks = days / 7;

   int months = weeks / 5;

   int years = months / 12;

   NSString *string;

  

     if(difference < 60)
     {
        string = [NSString stringWithFormat:@"%i seconds ago",difference];

     }

     else if (minutes > 1 && minutes < 60)
     {
        string = [NSString stringWithFormat:@"%i minutes ago",minutes];
     }

     else if (hours > 1 && hours < 24)
     {
        string = [NSString stringWithFormat:@"%i hours ago",hours];
     }

     else if (days > 1 && days < 7) 
     {
       string = [NSString stringWithFormat:@"%i days ago",days];
     }



    else if (weeks > 1 && weeks < 5) 

   {

       string = [NSString stringWithFormat:@"%i weeks ago",weeks];

   }
   else if (months > 1  && months < 12) 

   {
       string = [NSString stringWithFormat:@"%i months ago",months];


   }
   else if (years > 1 && years < 12) 

   {

      
    string = [NSString stringWithFormat:@"%i years ago",years];


   }

     return string;

}
Dhaval Panchal
  • 2,529
  • 1
  • 25
  • 36
kushalrshah
  • 638
  • 1
  • 5
  • 14
2

Try this

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
        //[df setDateStyle:NSDateFormatterMediumStyle];
        //df.dateFormat = @"yyyy-mm-dd";
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *date1 = [df dateFromString:@"2011-04-30"];
    NSDate *date2 = [df dateFromString:@"2011-05-03"];

NSLog(@"date1=%@",date1);
NSLog(@"date2=%@",date2);
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];

int numberOfDays = secondsBetween / 86400;

NSLog(@"There are %d days in between the two dates.", numberOfDays);

Adjust the time zone to get the correct dates...

visakh7
  • 26,380
  • 8
  • 55
  • 69
1

You must say the NSDate the format you're using:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"yyyy'-'MM'-'dd";
NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; // Your locale
df.locale = local;
[local release];
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; // Your timezone
NSDate *date1 = [df dateFromString:@"2011-04-30"];
NSDate *date2 = [df dateFromString:@"2011-05-03"];
[df release];
Ruben Marin
  • 1,639
  • 14
  • 24
0

Change

NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; 

to

NSTimeInterval secondsBetween = [date1 timeIntervalSinceDate:date2]; 
Tushar
  • 1,242
  • 1
  • 8
  • 19