查找两个日期之间的精确差异

发布于 2024-10-09 13:33:01 字数 896 浏览 3 评论 0原文

我想要对日期比较进行一些更改。

在我的应用程序中,我比较两个日期并获取天数差异,但如果只有一天差异,系统会显示 0 作为天数差异。

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];

我找到了一些解决方案,如果我们将时间设置为 00:00:00 进行比较,那么它会向我显示正确的答案,我不知道是对还是错。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世态炎凉 2024-10-16 13:33:02

我通常会找出以秒为单位的差异并计算 ceil(diffInSeconds / 86400) 。

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

雨后咖啡店 2024-10-16 13:33:02

尝试使用此代码获取两个不同的日期和时间。

  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);

我希望这段代码对你有用。

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.

捂风挽笑 2024-10-16 13:33:02

这里有一个完美的解决方案来查找两个日期之间的差异

- (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;
}

并使用两个参数作为 NSDate 调用上面的函数

NSString *duration = [self calculateDuration:postDate secondDate:[NSDate date]];
lblPostTime.text = duration;

注意:: postDate 是 FirstDate &第二个日期是当前日期..

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

很快妥协 2024-10-16 13:33:01

我认为这不正确,请将时间设置为 00:00:00。

可能您得到的差异小于 24 小时,这就是为什么四舍五入而您的时间为 0 天。

亚历山大的解决方案是正确的,所以使用像这样的解决方案 -

这对我来说也很好。

NSDate *endDate=[dateFormat dateFromString:now];
        NSTimeInterval interval = [CeremonyDate timeIntervalSinceDate:endDate];
        int diff=interval/86400;//for converting seconds into days.

同样的问题是对你在这里得到的数字进行四舍五入,但你可以用一种不稳定的方式解决这个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文