0

I'd like to check whether a NSDate is in the same week as today. Therefore I'm consider the weekOfYears from a NSDateComponents like mentioned in this post.

- (BOOL)dateIsThisWeek:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setFirstWeekday: 2];    // Added in second try

    NSDate *today = [NSDate date];
    NSDateComponents *todaysComponents = [cal components:NSCalendarUnitWeekOfYear fromDate: today];
    NSUInteger todaysWeek = [todaysComponents weekOfYear];

    NSDateComponents *otherComponents = [cal components:NSCalendarUnitWeekOfYear fromDate: date];
    NSUInteger anotherWeek = [otherComponents weekOfYear];

    return todaysWeek == anotherWeek;    // todaysWeek=7 | anotherWeek=8
}

In my example I've the following dates:

today:  2017-02-15 12:00:00 +0000
date:   2017-02-18 12:00:00 +0000

The problem is, that todaysWeek is set to 7 and anotherWeek to 8 and I can not imagine why.

I thought that the problem might be, that the week is starting on a Sunday, so I set the first weekday of the calendar to 2 (Monday), but unfortunately without any success.

UPDATE

I've also tried this code but unfortunately with the same result:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *todaysComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:[NSDate date]];
NSUInteger todaysWeek = [todaysComponents weekOfYear];
NSDateComponents *otherComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate: date];
NSUInteger datesWeek = [otherComponents weekOfYear];

return todaysWeek==datesWeek;    // todaysWeek=7 | datesWeek=8

Both dates should be in week number 7 like multiple websites are describing.

Community
  • 1
  • 1
user3191334
  • 1,148
  • 3
  • 15
  • 33
  • 1
    How do you define a "week"? Is it the current "named week" - that is, starting from last Sunday? Starting from last Monday? Is a week 7 consecutive days? So, if January 1 is on a Thursday, then Tuesday Jan 6 is still the first "week"? – DonMag Feb 15 '17 at 13:45
  • Possible duplicate of [How to check if NSDate is in current week?](http://stackoverflow.com/questions/38970836/how-to-check-if-nsdate-is-in-current-week) –  Feb 15 '17 at 13:46
  • @DonMag in my opinion a week is from Monday to Sunday like in this example: http://week-number.net/calendar-with-week-numbers-2017.html – user3191334 Feb 15 '17 at 13:51
  • @Sneak unfortunately your suggestion didn't solve my problem. Using the code marked as solved in the post, returns the same week numbers, 7 and 8. – user3191334 Feb 15 '17 at 13:54
  • @user3191334 I am not sure what the problem is, the current week today is 7, next week is 8, you can see that http://www.epochconverter.com/weeknumbers you should check this issue too maybe helps you in the right path , but I dont understand what the problem is exactly reading your question: http://stackoverflow.com/questions/16875420/nscalendar-why-does-setting-the-firstweekday-doesnt-effect-calculation-outcome –  Feb 15 '17 at 14:00
  • @Sneak So my second date, 18.02., which is in week 7 also verified by you link. `Week 07 is from Monday February 13, 2017 until (and including) Sunday February 19, 2017.` should not has the weekOfYear 8, like the code returning. – user3191334 Feb 15 '17 at 14:05
  • @user3191334 Check my answer –  Feb 15 '17 at 14:12

2 Answers2

1

Try this maybe? (Copied from my own project code)

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[gregorianCalendar setLocale:[NSLocale localeWithLocaleIdentifier:@"be_NL"]];

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:19];
[comps setMonth:02];
[comps setYear:2017];
NSDate *date = [gregorianCalendar dateFromComponents:comps];

//NSCalendar *cal = [NSCalendar currentCalendar];
//[gregorianCalendar setFirstWeekday:2];    // Added in second try

NSDate *today = [NSDate date];
NSDateComponents *todaysComponents = [gregorianCalendar components:NSCalendarUnitWeekOfYear fromDate: today];
//[todaysComponents setWeekdayOrdinal:2];

NSDateComponents *otherComponents = [gregorianCalendar components:NSCalendarUnitWeekOfYear fromDate: date];
//[otherComponents setWeekdayOrdinal:2];

// Not sure if needed to set timezone too depending on your local time, but you can set this for all dates if needed
//[otherComponents setTimeZone:[NSTimeZone localTimeZone]];

NSUInteger todaysWeek = [todaysComponents weekOfYear];
NSUInteger anotherWeek = [otherComponents weekOfYear];

NSLog(@"todays week %ld",(long)todaysWeek);
NSLog(@"anotherWeek %ld",(long)anotherWeek);

Prints out:

2017-02-17 03:51:58.231797 Sneak[5518:2676264] todays week 7
2017-02-17 03:51:58.231836 Sneak[5518:2676264] anotherWeek 7

EDIT: (As mentioned below in comment section)

Monday is the first day of the week according to the international standard ISO 8601, but in the US, Canada, and Japan it's counted as the second day of the week. Monday comes after Sunday and before Tuesday in our modern day Gregorian Calendar. - timeanddate.com/calendar/days/monday.html Hope "be_NL" never switches – DonMag

  • Thanks and sorry. I just copied your code and it's working now. I can't imagine the reason why I always got week 8. EDIT: First solution was enough but second one is working great as well. THANKS :) – user3191334 Feb 15 '17 at 14:17
  • @user3191334 NP, you can use it. Don't forget to accept my answer for the community :) –  Feb 15 '17 at 14:19
  • 1
    Are you guys *sure* this is doing what you want? If I understand correctly, @user3191334 wants Sunday, Feb 19 to be in the same week as today, Wednesday, Feb 15. With the above code, you're setting the comparison date to Saturday, 18 and you get weeks 7 & 7... If you run it with `[comps setDay:19]` you get weeks 7 & 8. Or? – DonMag Feb 15 '17 at 16:02
  • Thanks @DonMag for the hint. Yes I didn't notice that Sunday Feb 19th is in the 8th week according to the current code. Do you have an idea how to modify the code, so the 19th is in week 7 too? – user3191334 Feb 16 '17 at 20:46
  • 1
    @user3191334 You think ill dissapoint you? Check my updated answer :) –  Feb 17 '17 at 02:54
  • 1
    @Sneak Ahhh setting the locale is doing the trick! Thank you so much! – user3191334 Feb 17 '17 at 17:34
  • 1
    Monday is the first day of the week according to the international standard ISO 8601, but in the US, Canada, and Japan it's counted as the second day of the week. Monday comes after Sunday and before Tuesday in our modern day Gregorian Calendar. - https://www.timeanddate.com/calendar/days/monday.html Hope "be_NL" never switches :) – DonMag Feb 17 '17 at 20:42
  • @DonMag Exactly, great information to add to the answer. He can change the locale to whatever he chooses to fit for the moment of use. –  Feb 18 '17 at 02:26
0

This is a very slight modification to Sneak's code... Call it with:

//
// startDayOfWeek: Sunday == 1, Monday == 2, Tues == 3, etc

Bool b = [self dateIsThisWeek:date startDayOfWeek:2];


- (BOOL)dateIsThisWeek:(NSDate *)dateToTest startDayOfWeek:(NSInteger)iStart {

    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    // this is the key - 1 == week runs Sunday-Saturday, 2 == Monday-Sunday, 3 == Tues-Mon, etc
    [gregorianCalendar setFirstWeekday:iStart];

    NSDate *today = [NSDate date];

    NSDateComponents *todaysComponents = [gregorianCalendar components:NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday fromDate: today];
    NSInteger todaysWeek = [todaysComponents weekOfYear];

    NSDateComponents *testComponents = [gregorianCalendar components:NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday fromDate: dateToTest];
    NSInteger testsWeek = [testComponents weekOfYear];

    return todaysWeek == testsWeek;
}
DonMag
  • 69,424
  • 5
  • 50
  • 86