本文介绍了如何知道两个NSDate是否在同一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您知道如何知道两个 NSDate
是否在同一天。我想考虑一下语言环境......
Do you know how to know if two NSDate
are the same day. I want to take into account the locale...
可以很容易地使用 timeIntervalSinceDate:
但是星期一23H58和周二00H01不在同一天......
It could be easy to use a timeIntervalSinceDate:
but Monday 23H58 and Tuesday 00H01 are not in the same day...
处理 NSDate
和计算的区域设置不是非常容易。
Dealing with NSDate
and locale for calculation is not very easy.
推荐答案
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsForFirstDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:firstDate];
NSDateComponents *componentsForSecondDate = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:secondDate];
if ([componentsForFirstDate year] == [componentsForSecondDate year])
等等。
我不知道 isEquals
是否可以做你想要的 NSDateComponents
。
I don't know if isEquals
would do what you want on NSDateComponents
.
这篇关于如何知道两个NSDate是否在同一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!