3
是否有人可以帮助我弄清楚如何检查某日是否与今天同日。我想这需要在同一时区的同一天的0小时内创建一个日历日并检查,但到目前为止,我的尝试让我更加困惑于任何事情。检查日期是否在同一日历日
是否有人可以帮助我弄清楚如何检查某日是否与今天同日。我想这需要在同一时区的同一天的0小时内创建一个日历日并检查,但到目前为止,我的尝试让我更加困惑于任何事情。检查日期是否在同一日历日
NSCalendar让你处理人天。所以,你可以实现一个类别是这样的:
@implementation NSDate (IsItToday)
- (BOOL)isToday {
NSUInteger desiredComponents = NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
NSDateComponents *myCalendarDate = [[NSCalendar currentCalendar] components:desiredComponents fromDate:self];
NSDateComponents *today = [[NSCalendar currentCalendar] components:desiredComponents fromDate:[NSDate date]];
return [myCalendarDate isEqual:today];
}
@end
你应该好好读通过这个Dates and Times Programming Topics for Cocoa,但这样的事情应该工作:
NSDate *today = [NSDate date];
NSTimeInterval difference = [today timeIntervalSinceDate:otherDate];
NSTimeInterval secondsPerDay = 24 * 60 * 60;
if (difference < secondsPerDay)
{
//same day as today
}
,检查24小时,如果不是它的同日 – dizy 2009-06-09 06:08:00
你的答案是错误的(该问题问的日历天,没有时间间隔)。在告诉某人rtfm之前,请仔细阅读这个问题! – Christoph 2009-07-25 07:57:34