Find exact difference between two dates

女生的网名这么多〃 提交于 2019-12-02 05:24:44

问题


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 the system shows me 0 as a difference of days.

NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];
[date_formater setDateFormat:@"MMM dd,YYYY"];

NSString *now=[NSString stringWithFormat:@"%@",[date_formater stringFromDate:[NSDate date]]];
LblTodayDate.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",now]];

NSDate *dateofevent = [[NSUserDefaults standardUserDefaults] valueForKey:@"CeremonyDate_"];
NSDate *endDate =dateofevent;

NSDate *startDate = [NSDate date];

gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];

int days = [components day];

I found some solutions that If we make the time as 00:00:00 for comparison then it will show me proper answer, I am right or wrong I don't know.


回答1:


I usually find out difference in seconds and calculate ceil(diffInSeconds / 86400).




回答2:


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.




回答3:


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.




回答4:


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..



来源:https://stackoverflow.com/questions/4544406/find-exact-difference-between-two-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!