NSCalendar一周的第一天 [英] NSCalendar first day of week

查看:491
本文介绍了NSCalendar一周的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否有一种方法可以在NSCalendar中设置一周的第一天,或者有一个日历已将星期一作为一周的第一天,而不是星期日。
我目前正在开发一个基于一周工作量的应用程序,它需要从星期一开始,而不是从星期日开始。我最有可能做一些工作来解决这个问题,但会有很多角落的情况。



这里是一些代码,我使用。现在星期六,所以我希望工作日是6,而不是7,这意味着星期日将是7而不是滚动到0

  NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
[gregorian setFirstWeekday:0];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents * todaysDate = [gregorian components:unitFlags fromDate:[NSDate date]];
int dayOfWeek = todaysDate.weekday;


解决方案

编辑不检查在一周的开始在上个月开始的边缘情况。涵盖此内容的一些更新代码: http://stackoverflow.com/a/14688780/308315






如果任何人仍然注意这一点,您需要使用

  ordinalityOfUnit:inUnit:forDate:

并将firstWeekday设置为2 1 ==星期日和7 ==星期六)



以下是代码:

  NSCalendar * gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 
[gregorian setFirstWeekday:2]; //星期日== 1,星期六== 7
NSUInteger adjustedWeekdayOrdinal = [gregorian ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:[NSDate date]];
NSLog(@Adjusted weekday ordinal:%d,adjustedWeekdayOrdinal);

记住,工作日的序数从一周的第一天开始,而不是零。 / p>

文档链接


Does anyone know if there is a way to set the first day of the week on a NSCalendar, or is there a calendar that already has Monday as the first day of the week, instead of Sunday. I'm currently working on an app that is based around a week's worth of work, and it needs to start on Monday, not Sunday. I can most likely do some work to work around this, but there will be a lot of corner cases. I'd prefer the platform do it for me.

Thanks in advance

Here's some the code that I'm using. it's saturday now, so what I would hope is that weekday would be 6, instead of 7. that would mean that Sunday would be 7 instead of rolling over to 0

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:0];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *todaysDate = [gregorian components:unitFlags fromDate:[NSDate date]];
int dayOfWeek = todaysDate.weekday;

解决方案

Edit: This does not check the edge case where the beginning of the week starts in the prior month. Some updated code to cover this: http://stackoverflow.com/a/14688780/308315


In case anyone is still paying attention to this, you need to use

ordinalityOfUnit:inUnit:forDate:

and set firstWeekday to 2. (1 == Sunday and 7 == Saturday)

Here's the code:

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setFirstWeekday:2]; // Sunday == 1, Saturday == 7
NSUInteger adjustedWeekdayOrdinal = [gregorian ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:[NSDate date]];
NSLog(@"Adjusted weekday ordinal: %d", adjustedWeekdayOrdinal);

Remember, the ordinals for weekdays start at 1 for the first day of the week, not zero.

Documentation link.

这篇关于NSCalendar一周的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆