avuthless
avuthless

Reputation: 996

NSCalendar dateFromComponents returns wrong value when using weeks

So i try to get this weeks date range.

When i get current date components (only year and date) these are the values:

enter image description here

I noticed that there is "Obsolescent" written near week. Did try searching apple docs but found nothing about this.

Then i try to get the NSDate with current calendar (or gregorian calendar, same value) but the value i get is:

enter image description here

So weeks got ignored. So i added few months and one week to my components and had such components:

enter image description here

Then i tried to get my date out of this, this is what i got:

enter image description here

Now clearly week components are getting ignored here. Why is that so? Is it deprecated? Or is it some bug?

Upvotes: 2

Views: 820

Answers (3)

avuthless
avuthless

Reputation: 996

So i found the solution finally.

It appears that it all sorts out (starts working) if you just set weekday:

[startDate setWeekday:1];

Then the date with week is returned correctly.

Do not know if it is still a bug when week components get ignored without setting weekday.

Upvotes: 2

Martin R
Martin R

Reputation: 540075

To compute the start end date of the a week, the rangeOfUnit:startDate:interval:forDate: method of NSCalendar is quite useful:

NSDate *now = [NSDate date];

NSDate *startOfWeek;
NSTimeInterval length;
[[NSCalendar currentCalendar] rangeOfUnit:NSWeekCalendarUnit
                                startDate:&startOfWeek
                                 interval:&length
                                  forDate:now];
NSDate *endOfWeek = [startOfWeek dateByAddingTimeInterval:length];
NSLog(@"%@", startOfWeek);
NSLog(@"%@", endOfWeek);

To compute the same date a week before, use dateByAddingComponents:

NSDate *now = [NSDate date];
NSDateComponents *comp = [[NSDateComponents alloc] init];
[comp setWeek:-1]; // One week back in time
NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:now options:0];
NSLog(@"%@", now);
NSLog(@"%@", date);

Upvotes: 1

avuthless
avuthless

Reputation: 996

Even though i do not have an answer to my question i will post solution to this issue that i used.

So i took weeks and multiplied them with 7. This gave me current week end date. I know it is not good since weeks can have different days set, etc. But for my most default issue (gregorian calender, 7 days a week) this solution was fast workaround in already existing date time calculator.

[components setDay:components.week * 7 - 7];                                //Start of week
        startDate = [[NSCalendar currentCalendar] dateFromComponents:components];
        [components setDay:components.day + 7];                                     //End of week
        endDate = [[NSCalendar currentCalendar] dateFromComponents:components];
        [components setDay:0];                                                      //I am using this not just here so i am setting back my changes

Upvotes: 0

Related Questions