简体   繁体   English

给定周的第一个日期(NSDate)

[英]First date(NSDate) of the given week

I have a week number in int and I want to get the first date( NSDate value ) of that week.我在 int 中有一个周数,我想获得该周的第一个日期( NSDate 值)。

For an Example: First date ( sunday ) of the 21st week of 2011 is 2011-05-22例如:2011 年第 21 周的第一个日期(星期日)是 2011-05-22

I'm trying with NSCalendar, NSDateComponents to achieve this.我正在尝试使用 NSCalendar、NSDateComponents 来实现这一目标。 If anybody knows how to achieve this really appreciate your help.如果有人知道如何实现这一点,非常感谢您的帮助。

Try something like:尝试类似:

NSDate *now = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
[comp setWeekday:1];
[comp setWeek: 21];
NSDate *resultDate = [gregorian dateFromComponents:comp];

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

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