7

I have in the past used the below function to add on a specific time interval using NSDateComponents to an existing date.

(NSDate *)dateByAddingComponents:(NSDateComponents *)comps
                          toDate:(NSDate *)date
                         options:(NSCalendarOptions)opts

From iOS8 the week value is deprecated for NSDateComponents, which means I can't achieve what I want to do: generate a new NSDate by adding a certain number of weeks to a given NSDate.

Any help would be greatly appreciated.

nempoBu4
  • 6,521
  • 8
  • 35
  • 40
Rich
  • 882
  • 1
  • 8
  • 19

4 Answers4

18

Just use weekOfYear:

Apple docs for NSDateComponents week:

Deprecation Statement
Use weekOfYear or weekOfMonth instead, depending on what you intend.

NSDate *date = [NSDate date];
NSDateComponents *comp = [NSDateComponents new];
comp.weekOfYear = 3;
NSDate *date1 = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:date options:0];
NSLog(@"date:  %@", date);
NSLog(@"date1: %@", date1);

Output:

     
date:  2015-01-13 04:06:26 +0000  
date1: 2015-02-03 04:06:26 +0000

If you use week you get this warning:

'week' is deprecated: first deprecated in ... - Use weekOfMonth or weekOfYear, depending on which you mean

When using the weekOfMonth or weekOfYear as a delta they work the same. Where they are different is when they are used to obtain the week number where you will get the week of the month with a range of 6 or the week of the year with a range of 53.

zaph
  • 111,848
  • 21
  • 189
  • 228
10

Update: As Zaph said in his answer, Apple actually recommends using weekOfYear or weekOfMonth instead of the answer I provided. View Zaph's answer for details.


You'll probably quickly realize that you're overthinking it, but here's how you can add a certain number of weeks to a date even though the week value's been deprecated, ex:

NSDateComponents *comp = [NSDateComponents new];
int numberOfDaysInAWeek = 7;
int weeks = 3; // <-- this example adds 3 weeks
comp.day = weeks * numberOfDaysInAWeek;

NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:date options:0];
Lyndsey Scott
  • 37,080
  • 10
  • 92
  • 128
  • No problem. It happens :) – Lyndsey Scott Jan 13 '15 at 03:28
  • Sorry but this answer has the potential to be wrong since it assumes every week has exactly 7 days. The answer by @Zaph is safer. – rmaddy Jan 13 '15 at 04:51
  • @rmaddy No need to apologize. I didn't know of any cases when weeks don't have exactly 7 days (daylight savings perhaps?), nor did I realize there was a weekOfYear or weekOfMonth property; but if Apple says to use weekOfYear or weekOfMonth instead, that's what should be used. I'll research this a bit more to figure out the difference between weekOfYear and weekOfMonth then update accordingly. Thanks! – Lyndsey Scott Jan 13 '15 at 04:57
  • @LyndseyScott Admittedly I don't know an exact situation where using "days * 7" will give a different result than using "weekOfYear" but daylights savings and leap year (or leap seconds) come to mind. – rmaddy Jan 13 '15 at 05:00
  • When using `weekOfMonth` or `weekOfYear` to add or subtract from a date they work the same. Where they are different is when they are used to obtain the week number. – – zaph Jan 13 '15 at 23:27
0

I prefer to use dateByAddingUnit. It's more intuitive

return [NSDate[[NSCalendar currentCalendar] dateByAddingUnit:NSCalendarUnitMonth value:3 toDate:toDate options:0];
SureSight
  • 1
  • 1
-1

You can add a category on NSDate with the following method:

- (NSDate *) addWeeks:(NSInteger)weeks
{
    NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components=[[NSDateComponents alloc] init];
    components.day = weeks * 7;

    return [gregorian dateByAddingComponents:components toDate:self options:0];
}
Julian J. Tejera
  • 1,015
  • 10
  • 17
  • There was a reason Apple removed `week`, don't add it back to `NSDate` in another form when all that is needed is to use `weekOfYear` as the docs suggest. – zaph Jan 13 '15 at 04:17