Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 807
KlimczakM Avatar asked Sep 30 '14 11:09

KlimczakM


1 Answers

Use NSWeekOfMonthCalendarUnit or NSWeekOfYearCalendarUnit instead of NSWeekCalendarUnit.

like image 162
Parag Shinde Avatar answered Nov 06 '22 01:11

Parag Shinde