I'd like to check whether a NSDate is in the same week as today. Therefore I'm consider the weekOfYear
s 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.
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