使用一年中的一周检查NSDate是否在本周

时间:2017-02-15 13:40:16

标签: ios objective-c nscalendar

我想检查NSDate是否与今天同一周。因此,我会考虑post中提到的weekOfYear之类的NSDateComponents

- (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
}

在我的例子中,我有以下日期:

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

问题是,todaysWeek设置为7,anotherWeek设置为8,我无法想象为什么。

我认为问题可能是,本周从星期日开始,所以我将日历的第一个工作日设置为2(星期一),但遗憾的是没有任何成功。

更新

我也尝试过这段代码但遗憾的是结果相同:

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

两个日期应该在第7周,就像多个websites所描述的那样。

2 个答案:

答案 0 :(得分:1)

试试吧? (从我自己的项目代码中复制)

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);

打印出来:

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

编辑:(如评论部分所述)

根据国际标准ISO 8601,周一是本周的第一天,但​​在美国,加拿大和日本,它被视为一周的第二天。星期一是星期日之后,星期二之前是我们现代的格列高利历。 - timeanddate.com/calendar/days/monday.html希望“be_NL”永远不会切换 - DonMag

答案 1 :(得分:0)

这是对Sneak代码的非常轻微的修改...请将其命名为:

//
// 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;
}