来自 NSDateComponents 的有序工作日索引
我为 NSDate
创建了一个类别,它可以创建带有工作日名称的日期的字符串表示形式,例如 1 月 5 日星期四。
我想获取工作日索引并使用定制的工作日数组,这些数组存储在 plist 中并本地化为英语和西班牙语。
然而,使用 NSDateComponents 给了我一些意想不到的结果。我同时请求 NSWeekdayOrdinalCalendarUnit
和 NSWeekdayCalendarUnit
并且将结果输出到日志中,并且得到以下示例:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayOrdinalCalendarUnit | NSWeekdayCalendarUnit fromDate:self];
NSInteger weekdayOrdinal = [components weekdayOrdinal];
NSInteger weekdayNonOrdinal = [components weekday];
NSLog(@"Date: %@", [self description]);
NSLog(@"Device oridinal weekday: %d", weekdayOrdinal);
NSLog(@"Device non-oridinal weekday: %d", weekdayNonOrdinal);
2012-01-06 15:05:56.492 MyApp[964:11903] 日期:2012-01-05 16:13:46 +0000
2012-01-06 15:05:56.492 MyApp[964:11903] 设备原始工作日:1
2012-01-06 15:05:56.493 MyApp[964:11903] 设备非正常工作日:5
这一天应该是星期四,序数应该是 4,非序数 5
我不知道不明白 1 从哪里来。
我找到了一种方法来调整它,以确保我们使用带有 NSDateFormatter
的公历日历并指定 NSCalendar
(一周的第一个日期为星期一),例如以下:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setFirstWeekday:2];
NSUInteger adjustedWeekdayOrdinal = [gregorian ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self];
NSDateFormatter *weekdayF = [[[NSDateFormatter alloc] init] autorelease];
[weekdayF setDateFormat: @"EEEE"];
[weekdayF setCalendar:gregorian];
[weekdayF setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"]];
NSLog(@"The day of the week is: %@", [weekdayF stringFromDate:self]);
这将为我提供我想要的,但现在我必须将区域设置标识符存储在 plist 中才能在西班牙语中使用,我更愿意保留的是带有 NSDateComponents
的第一个实例,但我不这样做明白为什么我有时我会得到一个有趣的值。
我确实找到了使用此解决方案调整索引的解决方案: https://stackoverflow.com/a/3524462/662605
然而,说实话,我不想将其与 NSDateComponents
一起使用,我宁愿使用 NSDateFormatter
,但总的来说修复第一个示例会很酷,它给了我一个错误的 weekdayOrdinal
值。
对此有什么想法吗?
I have created a category for NSDate
which can create a string representation of a date with the weekday name, such as Thursday 5 January.
I want to get the weekday index and use a custom made array of weekdays which are stored in a plist and localised to english and spanish.
However using NSDateComponents
is giving me some unexpected results. I'm requesting both NSWeekdayOrdinalCalendarUnit
and NSWeekdayCalendarUnit
and I am outputting the results to the log and I am getting the following example:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayOrdinalCalendarUnit | NSWeekdayCalendarUnit fromDate:self];
NSInteger weekdayOrdinal = [components weekdayOrdinal];
NSInteger weekdayNonOrdinal = [components weekday];
NSLog(@"Date: %@", [self description]);
NSLog(@"Device oridinal weekday: %d", weekdayOrdinal);
NSLog(@"Device non-oridinal weekday: %d", weekdayNonOrdinal);
2012-01-06 15:05:56.492 MyApp[964:11903] Date: 2012-01-05 16:13:46 +0000
2012-01-06 15:05:56.492 MyApp[964:11903] Device oridinal weekday: 1
2012-01-06 15:05:56.493 MyApp[964:11903] Device non-oridinal weekday: 5
This day should be Thursday, ordinal should be 4 and non ordinal 5
I don't understand where the 1 comes from.
I found a way to adjust this to make sure we are using the gregorian calendar with NSDateFormatter
and specifying an NSCalendar
(with the first date of the week to be Monday) like the following:
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[gregorian setFirstWeekday:2];
NSUInteger adjustedWeekdayOrdinal = [gregorian ordinalityOfUnit:NSWeekdayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self];
NSDateFormatter *weekdayF = [[[NSDateFormatter alloc] init] autorelease];
[weekdayF setDateFormat: @"EEEE"];
[weekdayF setCalendar:gregorian];
[weekdayF setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"]];
NSLog(@"The day of the week is: %@", [weekdayF stringFromDate:self]);
This will give me what I want but now I have to store the locale identifier in the plist to use in spanish, what I would prefer to keep is the first instance with NSDateComponents
but I don't understand why I am getting a funny value sometimes.
I did find a solution to adjust the index with this solution: https://stackoverflow.com/a/3524462/662605
However, to be honest, I wouldn't like to use that along with NSDateComponents
, I would rather use the NSDateFormatter
, but overall it would be cool to fix the first example which gives me a false weekdayOrdinal
value.
Any ideas on this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您误解了
weekdayOrdinal
的含义。根据NSDateComponents
的文档,获取weekdayOrdinal
返回接收者的工作日单位序数,其中工作日序数单位是“下一个较大的工作日中的位置”日历单位,例如月份。例如,2 是该月第二个星期五的工作日序数单位”。根据这个推理,2012-01-05 的
weekdayOrdinal
正确地为 1 b/c,它是该月的第一个星期四。I think you're misinterpreting what
weekdayOrdinal
means. According to the doc forNSDateComponents
, getting theweekdayOrdinal
returns the ordinal number of weekday units for the receiver, where weekday ordinal units are the "position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month".By this reasoning, 2012-01-05 correctly has a
weekdayOrdinal
of 1 b/c it's the first Thursday of the month.