Reputation: 1795
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..
Upvotes: 0
Views: 380
Reputation: 648
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;
}
Upvotes: 3
Reputation: 1637
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];
Upvotes: 1
Reputation: 26400
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...
Upvotes: 2
Reputation: 1262
Change
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
to
NSTimeInterval secondsBetween = [date1 timeIntervalSinceDate:date2];
Upvotes: 0