获取给定年份和周数的一周中的第一个日期

发布于 12-24 21:15 字数 711 浏览 5 评论 0原文

我正在尝试获取一周中的第一个日期,并在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文