强制日历在第一周的工作日使用NSLocale

时间:2015-01-23 17:43:30

标签: objective-c date calendar

在iOS模拟器中,我需要强制第一个工作日依赖于当前的语言环境。

我已经尝试了好几次,但每次报告第一周都是1(星期日)。

在模拟器中,我尝试设置到德国,检查iOS日历并查看星期一;然后我去我的应用程序重新启动这个观点 - 它也应该说星期一;但它仍然报告星期天,第一个星期是1,那时应该是2。

如何解决此问题,以便始终强制为该给定区域设置的正确第一周工作日?

我尝试了这个解决方案:Finding the locale-dependent first day of the week但它似乎对我不起作用。

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setLocale:[NSLocale currentLocale]];
[calendar setFirstWeekday:[calendar firstWeekday]]; // Sunday == 1, Saturday == 7

[calendar setLocale:[NSLocale currentLocale]];
NSDateComponents *components = [calendar components:NSYearForWeekOfYearCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit fromDate:[NSDate date]];
[components setWeekday:[calendar firstWeekday]];
NSDate *firstDayOfWeek = [calendar dateFromComponents:components];

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"EEE"];

NSString *formatted = [f stringFromDate:firstDayOfWeek];
NSLog(@"Formatted = %@", formatted);

上述情况总是在周日报道。

这适用于区域设置:en(德国)和美国(en_US

有没有办法强迫第一个工作日依赖于语言环境,而我没有硬编码?

由于

0 个答案:

没有答案
相关问题