NSDateComponents:weekOfYear和weekOfMonth之间的差异 [英] NSDateComponents: difference between weekOfYear and weekOfMonth

查看:1438
本文介绍了NSDateComponents:weekOfYear和weekOfMonth之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我必须设置每周提醒,以便在一周后的同一天/时间发出警报。我一直使用NSDateComponenents.week = 1并添加到NSDate,使用NSCalendar的 dateByAddingComponents:toDate:options: API。似乎 week 现在在iOS7中被弃用,警告告诉我使用weekOfMonth或weekOfYear,取决于你的意思。但我找不到很多文档,关于它们是什么意思。

In my app, I have to set up a weekly reminder so that an alert goes off at the same day/time, a week from now. I have been using NSDateComponenents.week = 1 and adding that to an NSDate, using NSCalendar's dateByAddingComponents:toDate:options: API. It seems that week is now being deprecated in iOS7, and the warning tells me to "Use weekOfMonth or weekOfYear, depending on which you mean". But I can't find much documentation about what either of them means.

任何人都可以解释这两者之间的区别吗?在基本测试中,两者在添加到NSDate时返回相同的值,但我确定两者之间有一些有意义的差异。

Can anyone explain the difference between the two? In a basic test, the two return the same value when added to an NSDate, but I'm sure there is some meaningful difference between the two.

推荐答案

两者之间的区别显示了相对于月或年的星期。

The difference between the two show show the week relative to the month or year.

2月的第二周(weekOfMonth)将是年度的第七周(weekOfYear),例如,根据年份。

The second week (weekOfMonth) of February would be the seventh week (weekOfYear) of the year, for example, depending on the year.

至于使用日期组件在一个日期中添加周数,似乎并不重要,但使用了weekOfYear似乎是互联网上的首选。

As to using date components for adding weeks to a date, it doesn't appear to matter which is used but weekOfYear seems to be preferred on the Internet.

我在Swift操场上做了一些测试,他们在添加星期到一个日期时总是计算相同的值。

I did some testing in a Swift playground and they always calculated the same value when adding weeks to a date.

这篇关于NSDateComponents:weekOfYear和weekOfMonth之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆