【发布时间】:2011-06-13 21:05:14
【问题描述】:
我正在尝试查找日期是否为星期一。
为此,我这样做:
#define kDateAndHourUnitsComponents NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// for test and debug purpose
NSDateComponents* b = [calendar components:kDateAndHourUnitsComponents fromDate:retDate];
int a=[[calendar components:kDateAndHourUnitsComponents fromDate:theDate] weekday];
// -----------
if ([[calendar components:kDateAndHourUnitsComponents fromDate:theDate] weekday] == EKMonday) DoThis....
但这不起作用……a和b不包含任何有用的东西(a等于2147483647),
我也想知道 [calendar components:NSWeekdayCalendarUnit fromDate:retDate] 在这种情况下不再有用的用途......
我还发现这证实了这个过程: How to check what day of the week it is (i.e. Tues, Fri?) and compare two NSDates?
我错过了什么?
【问题讨论】:
-
在括号中包含宏定义总是一个好主意。像这样:
#define kDateAndHourUnitsComponents (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)。否则很容易引入一些非常讨厌的难以发现的问题。 -
好的,但是这些总是以这种方式使用,没有括号
标签: iphone compare nsdate dayofweek weekday