KlimczakM
KlimczakM

Reputation: 12914

NSDateComponents week is working differently on iOS8

NSDateComponents week is deprecated since iOS8. Apple suggest using weekOfMonth or weekOfYear instead, depending on the context, but they are working in a different way. For example:

components1 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
                          fromDate:[self date7DaysAgo]
                            toDate:[NSDate date]
                           options:0];

components1 returns month = 0, week = 1, day = 0, weekOfMonth and weekOfYear are both equal to 2147483647 (integer maximum value).

components2 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
                          fromDate:[self date30DaysAgo]
                            toDate:[NSDate date]
                           options:0];

components2 returns month = 1 (I know it depends, but its just an example), week = 0, day = 0, weekOfMonth and weekOfYear are both equal to 2147483647 (integer maximum value).

Week returns number of weeks during some period (if it was a week or it multiple) and it is completely different than number of a week in month or year. Why would Apple suggest using this instead?

Upvotes: 6

Views: 1283

Answers (1)

Parag Shinde
Parag Shinde

Reputation: 176

Use NSWeekOfMonthCalendarUnit or NSWeekOfYearCalendarUnit instead of NSWeekCalendarUnit.

Upvotes: 6

Related Questions