找到两个日期之间的确切差异 [英] Find exact difference between two dates

查看:87
本文介绍了找到两个日期之间的确切差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在日期比较中做一些更改。

I want some changes in the date comparison.

在我的申请中,我比较两个日期并获得天数,但如果只有一天差异系统显示0作为天数的差异。

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

我找到了一些解决方案如果我们把时间作为00:00:00进行比较那么它会告诉我正确答案,我是对还是错,我不知道。

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.

推荐答案

我通常会发现秒数差异并计算 ceil(diffInSeconds / 86400)

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

这篇关于找到两个日期之间的确切差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆