今日在添加感恩节(11月的第四个星期四)节日提醒时,参考了一下父亲节和母亲节。然后发现代码中,添加父亲节母亲节的本地提醒和提醒响应界面中remindArray中处理不一样。
父亲节母亲节在本地提醒中
#define CALENDAR_UNIT_FLAGS (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
NSDateComponents *fathersDC = [[NSDateComponents alloc]init];
[fathersDC setYear:todayDc.year];
[fathersDC setMonth:6];
[fathersDC setWeekday:1];//1~7,1表示周日
[fathersDC setWeekdayOrdinal:3];//表示第三个周几,不一定是这个月的第三周,可能是第四周
在提醒响应界面中remindArray
NSDateComponents *fatherDC = [[NSDateComponents alloc]init];
fatherDC.year = todayDC.year;
fatherDC.month = 6;
fatherDC.weekOfMonth = 3;
fatherDC.weekday = 1;
NSDate *fatherDate = [[OICalendarUtil currentCalendar]dateFromComponents:fatherDC];
fatherDC = [[OICalendarUtil currentCalendar]components:CALENDAR_UNIT_FLAGS fromDate:fatherDate];
NSDateComponents *motherDC = [[NSDateComponents alloc]init];
motherDC.year = todayDC.year;
motherDC.month = 5;
motherDC.weekOfMonth = 2;
motherDC.weekday = 1;
NSDate *motherDate = [[OICalendarUtil currentCalendar]dateFromComponents:motherDC];
motherDC = [[OICalendarUtil currentCalendar]components:CALENDAR_UNIT_FLAGS fromDate:motherDate];
在比较中发现,其中NSDateComponents的参数设置的不一样,参数分别是weekdayOrdinal和weekdayOfMonth,但是得到的结果竟然是同一天。
例如 weekday = 1;//1~7,1表示周日 weekdayOrdinal = 2, weekdayOfMonth = 2;
weekdayOrdinal 表示 这是这个月的第2个周日,可能是这个月的第二周,可能是第三周
weekdayOfMonth 表示 第2周的周日,其中MinimumDaysInFirstWeek会影响到weekdayOfMonth的判断 这个参数是设置的一周最少天数,如果设置成 7天,那么如果本月的1、2、3和上月的日期连在一周中,如果本月日期<weekdayOfMonth的设置,那么这个第一周不算。从下周开始算第一周。
所以类似于父亲节母亲节感恩节这种第几个星期几应该使用weekdayOrdinal。