如何确定NSDate是否是今天? [英] How to determine if an NSDate is today?
问题描述
如何检查 NSDate
是否属于今天?
我用来检查前10个字符从 [aDate description]
。 [[aDate description] substringToIndex:10]
返回字符串,如YYYY-MM-DD
与 [[[[NSDate date]] description] substringToIndex:10]
您可以比较日期元件:
NSDateComponents * otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
NSDateComponents * today = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
if([today day] == [otherDay day]&&
[today month] == [otherDay month]&&
[today year] == [otherDay year]&&
[today era] == [otherDay era]){
// do stuff
}
编辑:
我更喜欢stefan的方法,我认为这使得更清洁,更容易理解if语句:
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents * components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)fromDate:[NSDate date]];
NSDate * today = [cal dateFromComponents:components];
components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)fromDate:aDate];
NSDate * otherDate = [cal dateFromComponents:components];
if([today isEqualToDate:otherDate]){
// do stuff
}
Chris,我已经收录您的建议。我不得不查找什么时代,所以对于不知道的人,它区分BC和AD。这对大多数人来说可能是不必要的,但它很容易检查和添加一些确定性,所以我已经包括它。如果你要去速度,这可能不是一个好的方法。
How to check if an NSDate
belongs to today?
I used to check it using first 10 characters from [aDate description]
. [[aDate description] substringToIndex:10]
returns string like "YYYY-MM-DD"
so I compared the string with the string returned by [[[NSDate date] description] substringToIndex:10]
.
Is there more fast and/or neat way to check?
Thanks.
You can compare date components:
NSDateComponents *otherDay = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
if([today day] == [otherDay day] &&
[today month] == [otherDay month] &&
[today year] == [otherDay year] &&
[today era] == [otherDay era]) {
//do stuff
}
Edit:
I like stefan's method more, I think it makes for a cleaner and more understandable if statement:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:[NSDate date]];
NSDate *today = [cal dateFromComponents:components];
components = [cal components:(NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:aDate];
NSDate *otherDate = [cal dateFromComponents:components];
if([today isEqualToDate:otherDate]) {
//do stuff
}
Chris, I've incorporated your suggestion. I had to look up what era was, so for anyone else who doesn't know, it distinguishes between BC and AD. This is probably unnecessary for most people, but it's easy to check and adds some certainty, so I've included it. If you're going for speed, this probably isn't a good method anyway.
这篇关于如何确定NSDate是否是今天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!