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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
在我看来,此设置应该根据用户区域设置是动态的。
因此,应该使用:
这将使日历根据用户区域设置自动设置第一个工作日。 除非您正在为特定目的/用户区域设置开发应用程序(或者更愿意允许用户选择这一天)。
In my opinion this settings should be dynamic according to the user locale.
Therefore one should use:
This will cause the calendar to set the first week day according to the user locale automatically. Unless you are developing your app for a specific purpose/user locale (or prefer to allow the user to choose this day).
我就是这样做的。
I've done it like this.
我在这里的很多答案都遇到了麻烦。 。 也许只是我。 。
这是一个对我有用的答案:
更新:
正如所指出的(其他地方)作者:@vikingosegundo,一般来说,最好让当地人确定哪一天是一周的开始,但是在这个如果OP要求一周的开始发生在星期一,因此我们复制系统日历,并覆盖firstWeekDay。
I had trouble with a lot of the answers here. . maybe it was just me. .
Here's an answer that works for me:
Update:
As pointed out (elsewhere) by @vikingosegundo, in general its best to let the local determine which day is the start of the week, however in this case the OP was asking for the start of the week to occur on Monday, hence we copy the system calendar, and override the firstWeekDay.
克里斯的答案的问题在于边缘情况,即一周的开始是从上个月开始的。 这是一些更简单的代码,它还检查边缘情况:
The problem with Kris' answer is the edge case where the beginning of the week starts in the prior month. Here's some easier code and it also checks the edge case:
我在其他消息中看到了误解。 第一个工作日,无论是哪一个,其数字都是 1 而不是 0。默认情况下 Sunday=1,如“Cocoa 日期和时间编程指南简介:日历计算”中所示:
对于作为第一个工作日的星期一,我唯一的补救措施是使用蛮力条件来修复计算
I see misunderstanding in the other messages. The first weekday, whichever it is, has number 1 not 0. By default Sunday=1 as in the "Introduction to Date and Time Programming Guide for Cocoa: Calendrical Calculations":
For the Monday as a first workday the only remedy I have is brute force condition to fix the calculation
下面还涵盖了边缘情况,
Below also covers the edge case,
您只需更改日历的
.firstWeekday
即可。然后使用
rangeOfUnit:startDate:interval:forDate:
获取第一天You can just change
.firstWeekday
of the calendar.Then use
rangeOfUnit:startDate:interval:forDate:
to get the first day尝试这个:
Try this:
Iv 找到了使用 nscalender 显示任何工作日名称的方法..使用以下代码..
只需从 xcode 菜单栏打开控制台即可查看结果。复制将以下代码粘贴到 viewDidLoad 方法中以获取一周的第一天
输出将是:
因为我在 2/11/2010 上运行了此程序,所以您将得到所需的输出取决于当前日期。
同样,如果您想获取一周的第一个工作日,即星期一的日期,则只需稍微修改代码即可:
CHANGE :
[components setDay:([components day]-([components weekday]-1))];至
[components setDay:([components day]-([components weekday]-2))];
获取该周的星期一日期。
同样,您可以尝试查找七个工作日中任意一个的日期通过更改整数 -1、-2 等等...
希望你的问题得到解答..
谢谢,
邦森迪亚斯
Iv found out the way to display any weekday name using nscalender..using the following code..
Just open your console from xcode menu bar to see the results.Copy Paste the following code in your viewDidLoad method to get the first day of the week
The Output will be:
since i had run this program on 2/11/2010 u will get the desired output depending on the current date.
Similarly if u want to get the first working day of the week i.e Monday's date then just modify the code a bit:
CHANGE :
[components setDay:([components day]-([components weekday]-1))];
TO
[components setDay:([components day]-([components weekday]-2))];
to get Mondays date for that week..
Similarly u can try to find the date of any of seven workdays by changing the integer -1,-2 and so on...
Hope u r question is answered..
Thanks,
Bonson Dias
默认情况下,ISO 8601 日历的第一个工作日似乎设置为星期一。
The ISO 8601 calendar appears to have it's first weekday set to monday by default.
使用日历 nextWeekend(iOS 10 或更高版本)和 序数(感谢@kris-markel)。 我已将星期一作为 en_US 日历的一周第一天。
下面是一个回退到firstWeekday 的示例:
Using the Calendar nextWeekend (iOS 10 or later) and ordinality (thanks @kris-markel). I've gotten Monday as first of the week for the en_US calendar.
Here is an example of it with fallback to firstWeekday:
Swift 解决方案(注意,使用 .yearForWeekOfYear,而不是 .year):
The Swift solution (note, use .yearForWeekOfYear, not .year):
总有一天,会有的。
Someday, there will be.
我的简单方法是让星期一 = 0,星期日 = 6:
My simple way of doing this is to get Monday = 0, Sunday = 6:
编辑:这不会检查一周的开始于上个月开始的边缘情况。 一些更新的代码来解决这个问题:https://stackoverflow.com/a/14688780/308315
以防有人仍在付款注意这一点,您需要使用
firstWeekday并将其设置为2。(1 == Sunday and 7 == Saturday)
代码如下:
请记住,工作日的序数从一周的第一天开始,而不是零。
文档链接。
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: https://stackoverflow.com/a/14688780/308315
In case anyone is still paying attention to this, you need to use
and set firstWeekday to 2. (1 == Sunday and 7 == Saturday)
Here's the code:
Remember, the ordinals for weekdays start at 1 for the first day of the week, not zero.
Documentation link.
此代码构造一个设置为本周星期一的日期:
This code constructs a date that is set to Monday of the current week:
setFirstWeekday: 在 NSCalendar 对象上。
设置接收者第一个工作日的索引。
应该做到这一点。
setFirstWeekday: on the NSCalendar object.
Sets the index of the first weekday for the receiver.
Should do the trick.