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.
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.
The second week (weekOfMonth) of February would be the seventh week (weekOfYear) of the year, for example, depending on the year.
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.
I did some testing in a Swift playground and they always calculated the same value when adding weeks to a date.
I've always found that weeks seem to be handled strangely in iOS.
weekOfMonth
will be 1, 2, 3, 4 etc...
weekOfYear
will be 1 to 52 (I think)
They don't seem to be measurements of duration. Just like 'The 3rd of April' is not a measurement of duration whereas "1 day" is a measurement of duration.
The best way to add one weeks would be...
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:7];
NSDate *date = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
EDIT
After a little test I was right.
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear fromDate:date];
Printing description of date:
2014-08-20 08:15:13 +0000
Printing description of comps:
<NSDateComponents: 0xb7753c0>
Week of Year: 34
Week of Month: 4
The difference is useful if you want to set an event to always happen on "the second Tuesday of the month", for example.
In that case you would want weekOfMonth to be 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With