NSDateComponents 中weekdayOrdinal和weekdayOfMonth的区别

今日在添加感恩节(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。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值