简体   繁体   English

如何使用周数获​​取一周的星期一日期

[英]How to get the Monday date of a week using the week number

How can I find out the Date of Monday if I pass the week number. 如果我通过周数,我怎么能找到星期一的日期。 For example, I need the Monday of the Week 24 of the year 例如,我需要一年中第24周的星期一

Fill out a NSDateComponents object with the information you have. 使用您拥有的信息填写NSDateComponents对象。 Then you can ask the current NSCalendar what date corresponds to those components. 然后,您可以询问当前的NSCalendar与这些组件对应的日期。

NSDateComponents *components = [NSDateComponents new];
components.weekday = 2;
components.weekOfYear = 24;
components.year = 2012;
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents: components];

The weekday property starts with Sunday = 1. weekday属性从星期日= 1开始。

If you are on OS X 10.7 or newer you should use the property yearForWeekOfYear instead of year . 如果您使用的是OS X 10.7或更高版本,则应使用属性yearForWeekOfYear而不是year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM