I want some changes in the date comparison.
In my application I am comparing two dates and getting difference as number of Days, but if there is only one day difference
I usually find out difference in seconds and calculate ceil(diffInSeconds / 86400)
.
Here a prefect solution to find difference between two dates
- (NSString *)calculateDuration:(NSDate *)oldTime secondDate:(NSDate *)currentTime
{
NSString *timeSincePost;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:oldTime toDate:currentTime options:0];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
if (year) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)year,[[kAppDelegate languageBundle] localizedStringForKey:@"y" value:@"" table:nil]];
}
else if (month) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)month,[[kAppDelegate languageBundle] localizedStringForKey:@"M" value:@"" table:nil]];
}
if(day) {
timeSincePost = [NSString stringWithFormat:@"%ld%@", (long)day,[[kAppDelegate languageBundle] localizedStringForKey:@"d" value:@"" table:nil]];
}
else if(hour) {
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)hour,[[kAppDelegate languageBundle] localizedStringForKey:@"H" value:@"" table:nil]];
}
else if(minute) {
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)minute,[[kAppDelegate languageBundle] localizedStringForKey:@"m" value:@"" table:nil]];
}
else if(second)
timeSincePost = [NSString stringWithFormat: @"%ld%@", (long)second,[[kAppDelegate languageBundle] localizedStringForKey:@"s" value:@"" table:nil]];
return timeSincePost;
}
and call above function with two parameter as NSDate
NSString *duration = [self calculateDuration:postDate secondDate:[NSDate date]];
lblPostTime.text = duration;
note:: postDate is FirstDate & second date is current date..
Try this code for get two date and time different.
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:@"mm:ss"];
NSDate* firstDate = [dateFormatter dateFromString:@"04:45"];
NSDate* secondDate = [dateFormatter dateFromString:@"05:00"];
NSTimeInterval timeDifference = [secondDate timeIntervalSinceDate:firstDate];
NSLog(@"%f",timeDifference);
i hope this code usefull for you.
i think this is not correct make the time 00:00:00.
may be you get difference is less than 24 hour thats why it rounded off and you 0 day.
Alexander solution is right so use that solution like -
this works fine for me also.
NSDate *endDate=[dateFormat dateFromString:now];
NSTimeInterval interval = [CeremonyDate timeIntervalSinceDate:endDate];
int diff=interval/86400;//for converting seconds into days.
same problem of rounding a figure you get here but you can sort out that in an understable way.