获取给定年份和周数的一周中的第一个日期
我正在尝试获取一周中的第一个日期,并在 Objective-c 中给出年份和周数。 我想考虑荷兰日历。星期一是这里一周的第一天。 因此,就像在 MS Outlook 中一样,当我传递 2012 和 1 作为周数时,我希望我的函数返回 2012-01-02。
这是我尝试过的:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.year = 2012;
comps.weekOfYear = 1;
comps.weekday = 2;
NSDate *date = [cal dateFromComponents:comps];
我希望日期现在是 2012-01-02 ...,但它返回 2012-12-30 23:00:00。当我将 comps.weekday 更改为 1 时,它甚至返回我 2013-01-05 23:00:00??!!
这对我来说根本没有意义。谁能给我一些解释,或者给我正确的代码来使用?
回答:
这对我来说是解决方案:
[components setWeekOfYear:weekNr];
[components setYearForWeekOfYear:year];
[components setWeekday:2];
I'm trying to get the first date in a week giving a year and weeknumber in objective-c.
I want to take the dutch calendar into account. Monday is the first day in the week here.
So just as it is in MS Outlook I would like my function to return 2012-01-02 when i pass 2012 and 1 for the week number to it.
This is what I have tried:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.year = 2012;
comps.weekOfYear = 1;
comps.weekday = 2;
NSDate *date = [cal dateFromComponents:comps];
I would expect that date would now be 2012-01-02 ..., but instead it returns 2012-12-30 23:00:00. And when I change comps.weekday to 1, it is even returning me 2013-01-05 23:00:00??!!
This makes no sense to me at all. Could anyone provide me some explanation on this or perhaps give me the correct code to use?
ANSWER:
This was the solution for me:
[components setWeekOfYear:weekNr];
[components setYearForWeekOfYear:year];
[components setWeekday:2];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论