我需要知道两个NSDate实例是否都来自同一天。
有没有比获取NSDateComponents和比较日/月/年更容易/更好的方法呢?
Answers:
NSDateComponents听起来对我来说是最好的选择。尝试的另一种策略是将其免费桥接到CFDate,然后使用CFDateGetAbsoluteTime并进行减法以获得两个日期之间的时间量。但是,您必须做一些附加的数学运算才能确定时差是否将日期置入同一天。
NSDate
有一个不错的方法:timeIntervalSinceDate:
因此,您甚至不需要corefoundation。
如果您的目标是iOS 8(和OS X 10.9)或更高版本,则Joe的答案是使用NSCalendar中的新方法的更好解决方案,其目的仅在于:
-[NSCalendar isDate:inSameDayAsDate:]
对于iOS 7或更早版本:NSDateComponents是我的偏爱。这样的事情怎么样:
- (BOOL)isSameDayWithDate1:(NSDate*)date1 date2:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
return [comp1 day] == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year] == [comp2 year];
}
-(BOOL)isDate:(NSDate*)date1 sameDayAsDate:(NSDate*)date2
如果您使用的是iOS 8,则可以使用 -isDate:inSameDayAsDate:
。
来自NSCalendar.h
:
/*
This API compares the Days of the given dates, reporting them equal if they are in the same Day.
*/
- (BOOL)isDate:(NSDate *)date1 inSameDayAsDate:(NSDate *)date2 NS_AVAILABLE(10_9, 8_0);
请注意,由于某种原因,这并未进入Apple开发人员网站上的官方文档。但这绝对是公共API的一部分。
(注意:请查看Joe的答案,这是在iOS 8+上执行此操作的好方法)
我只是使用日期格式化程序:
NSDateFormatter *dateComparisonFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateComparisonFormatter setDateFormat:@"yyyy-MM-dd"];
if( [[dateComparisonFormatter stringFromDate:firstDate] isEqualToString:[dateComparisonFormatter stringFromDate:secondDate]] ) {
…
}
HTH。
我喜欢progrmr的解决方案,但是我会走得更远,并NSDate
提供一种提供这种方法的解决方案。这将使您的代码更具可读性,并且您无需将方法复制并粘贴到可能需要的每个新类中-只需导入头文件即可。
@interface NSDate (SameDay)
- (BOOL)isSameDayAsDate:(NSDate*)otherDate;
@end
#import "NSDate+SameDay.h"
@implementation NSDate (SameDay)
- (BOOL)isSameDayAsDate:(NSDate*)otherDate {
// From progrmr's answer...
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:self];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:otherDate];
return [comp1 day] == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year] == [comp2 year];
}
@end
然后,在导入NSDate+SameDay.h
您的类之后,可以像这样使用它:
if ([myFirstDate isSameDayAsDate:mySecondDate]) {
// The two dates are on the same day...
}
我使用NSDateComponents去除时间方面,然后进行比较。就像是:
if ([[self beginningOfDay:date1] isEqualToDate:[self beginningOfDay:date2]])
{
...
}
- (NSDate *)beginningOfDay:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comp = [calendar components:unitFlags fromDate:date];
return [calendar dateFromComponents:comp];
}
有没有比获取NSDateComponents和比较日/月/年更容易/更好的方法呢?
是的,有一种更简单的方法
-[NSCalendar rangeForUnit:startDate:interval:forDate:]
通过这种方法,可以轻松地将日期设置为单位的开始日期(天,小时,月,年等),并获取该单位的长度(间隔)。
作为一个类别,它可能像
@interface NSDate (Comparison)
-(BOOL) isSameDay:(NSDate *)rhs;
@end
@implementation NSDate (Comparison)
-(BOOL)isSameDay:(NSDate *)rhs
{
NSDate *lhs = self;
NSDate *lhsStart;
NSDate *rhsStart;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSCalendarUnitDay startDate:&lhsStart interval:NULL forDate:lhs];
[cal rangeOfUnit:NSCalendarUnitDay startDate:&rhsStart interval:NULL forDate:rhs];
return [lhsStart compare:rhsStart] == NSOrderedSame;
}
@end
此类别适用于所有iOS版本和Mac OS X 10.5+。
对于iOS 8+和Mac OS X 10.9+,您可以使用NSCalendars
- (BOOL)isDate:(NSDate *)date1 equalToDate:(NSDate *)date2 toUnitGranularity:(NSCalendarUnit)unit;
我创建自己的实用程序类。
@interface ZYUtility : NSObject
+ (NSDate *)yesterday;
+ (NSDate *)tomorrow;
+ (NSDate *)endOfDay:(NSDate *)date;
+ (NSDate *)beginningOfDay:(NSDate *)date;
@end
+ (NSDate *)yesterday;
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componets = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:[NSDate date]];
componets.day -= 1;
componets.hour = 24;
componets.minute = 0;
componets.second = 0;
return [calendar dateFromComponents:componets];
}
+ (NSDate *)tomorrow;
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componets = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:[NSDate date]];
componets.day += 1;
componets.hour = 0;
componets.minute = 0;
componets.second = 0;
return [calendar dateFromComponents:componets];
}
+ (NSDate *)beginningOfDay:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comp = [calendar components:unitFlags fromDate:date];
return [calendar dateFromComponents:comp];
}
+ (NSDate *)endOfDay:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [NSDateComponents new];
components.day = 1;
NSDate *theDate = [calendar dateByAddingComponents:components
toDate:[ZYUtility beginningOfDay:date]
options:0];
theDate = [theDate dateByAddingTimeInterval:-1];
return theDate;
}
以下内容将测试两个日期是否代表给定时代的同一天(在许多情况下足够):
- (BOOL)isDate:(NSDate *)date1 sameDayAsDate:(NSDate *)date2 {
NSCalendar *calendar = [NSCalendar currentCalendar];
int diff = [calendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSEraCalendarUnit
forDate:date1] -
[calendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSEraCalendarUnit
forDate:date2];
return 0 == diff;
}