查找两个日期之间的精确差异
我想要对日期比较进行一些更改。
在我的应用程序中,我比较两个日期并获取天数差异,但如果只有一天差异,系统会显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通常会找出以秒为单位的差异并计算 ceil(diffInSeconds / 86400) 。
I usually find out difference in seconds and calculate
ceil(diffInSeconds / 86400)
.尝试使用此代码获取两个不同的日期和时间。
我希望这段代码对你有用。
Try this code for get two date and time different.
i hope this code usefull for you.
这里有一个完美的解决方案来查找两个日期之间的差异
并使用两个参数作为 NSDate 调用上面的函数
注意:: postDate 是 FirstDate &第二个日期是当前日期..
Here a prefect solution to find difference between two dates
and call above function with two parameter as NSDate
note:: postDate is FirstDate & second date is current date..
我认为这不正确,请将时间设置为 00:00:00。
可能您得到的差异小于 24 小时,这就是为什么四舍五入而您的时间为 0 天。
亚历山大的解决方案是正确的,所以使用像这样的解决方案 -
这对我来说也很好。
同样的问题是对你在这里得到的数字进行四舍五入,但你可以用一种不稳定的方式解决这个问题。
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.
same problem of rounding a figure you get here but you can sort out that in an understable way.