更改 NSDateFormatter 中一周的第一天
为了获取一周中的几天,我使用:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *weekdays = [dateFormatter shortWeekdaySymbols];
Weekdays 为我提供了一个包含日期名称的数组,但它从星期日开始。由于某些原因,我希望该数组在周一或周日开始,具体取决于设备的本地化设置。
有办法做到吗?
In order to get the days of the week I use:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *weekdays = [dateFormatter shortWeekdaySymbols];
Weekdays gives me an array with the days names but it begins by Sunday. For some reasons I want this array to begin by Monday or Sunday depending on the localisation settings of the device.
Is there a way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以从具有当前区域设置的
NSCalendar
对象的-firstWeekday
方法获取当前区域设置的第一个工作日的从 1 开始的索引。然后你可以相应地修改你的周名称数组:我没有 iPhone SDK,但据我所知,这些 API 应该都可用,并且行为方式与 OS X 上相同。
You can get the 1-based index of the first weekday of the current locale from the
-firstWeekday
method of anNSCalendar
object with the current locale. Then you can modify your week names array accordingly:I don't have the iPhone SDK but AFAIK these APIs should be all available there and behave the same way as on OS X.
使用 Swift 实现了类似于 hasseg 的结果:
Achieved a result similar to hasseg in Swift using:
类似于 myexec 解决方案,但更通用且同样简洁
like the myexec solution but more generic and equally concise
您需要使用NSCalender以编程方式更改一周的开始日期。
You need to use NSCalender to change the start day of your week from programatically.