在iOS上查找当天的星期一

时间:2011-02-03 02:26:53

标签: iphone ios nsdate

我有一种情况,我需要在某个日期之前的星期一(这里的某个日期是localtournament.entryDeadline)。我尝试了以下,但它给了我同样的日期。我发现了一些类似的问题,但没有一个可以在iOS上做到这一点。它似乎应该很容易,我实际上喜欢Cocoa日期框架,但我现在很难过。

我做错了什么?结果(应该始终是星期一而不是)在localTournament.entryOpen中。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:localTournament.entryDeadline]; //1=sunday, 2=monday, etc

if(comps.weekday > 2) {
    //later in same week
    [comps setWeekday:2];
    [comps setHour:12];
    localTournament.entryOpen = [calendar dateFromComponents:comps];
} else if(comps.weekday == 1) {
    //sunday, go back a week
    [comps setWeek:-1];
    [comps setWeekday:2];
    [comps setHour:12];
    localTournament.entryOpen = [calendar dateByAddingComponents:comps toDate:localTournament.entryDeadline  options:0];

} else {
    //already monday, use previous week
    [comps setWeek:-1];
    [comps setHour:12];
    localTournament.entryOpen = [calendar dateByAddingComponents:comps toDate:localTournament.entryDeadline  options:0];
}

[calendar release];

3 个答案:

答案 0 :(得分:1)

如果同时指定NSWeekdayCalendarUnitNSDayCalendarUnit组件,则NSCalendar方法将优先于工作日信息(即未更新)日信息(即)。我要么完全删除NSDayCalendarUnit并将其替换为NSWeekCalendarUnit,要么更新当天而不是工作日。

有关详细信息,请参阅dateFromComponents:的文档。

答案 1 :(得分:1)

解决方案(下面)只是确定需要多少天才能减去。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [calendar components:(NSYearCalendarUnit | NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:localTournament.entryDeadline]; //1=sunday, 2=monday, etc

if(comps.weekday > 2) {
    //later in same week
    [comps setHour:12];
    localTournament.entryOpen = [calendar dateFromComponents:comps];

    NSDateComponents *sparseComps = [[[NSDateComponents alloc] init] autorelease];      
    [sparseComps setDay:0-(comps.weekday-2)];

    localTournament.entryOpen = [calendar dateByAddingComponents:sparseComps toDate:localTournament.entryOpen  options:0];
} else if(comps.weekday == 1) {
    //sunday, go back a week and add a day
    [comps setHour:12];
    localTournament.entryOpen = [calendar dateFromComponents:comps];

    NSDateComponents *sparseComps = [[[NSDateComponents alloc] init] autorelease];
    [sparseComps setWeek:-1];
    [sparseComps setWeekday:1];
    localTournament.entryOpen = [calendar dateByAddingComponents:sparseComps toDate:localTournament.entryOpen  options:0];
} else {
    //already monday, use previous week
    [comps setHour:12];
    localTournament.entryOpen = [calendar dateFromComponents:comps];

    NSDateComponents *sparseComps = [[[NSDateComponents alloc] init] autorelease];
    [sparseComps setWeek:-1];
    localTournament.entryOpen = [calendar dateByAddingComponents:sparseComps toDate:localTournament.entryOpen  options:0];
}

[calendar release];

答案 2 :(得分:0)

要推断Justin的答案,如果您想要使用周和工作日,则应创建一个新的NSDateComponent对象,其中包含weekdayweekOfYear个元素INSTEAD {{ 1}}和day个组件。您可能需要一个新的临时对象。

以下是我的项目中的一些代码,它在下周五发现,但它也有类似的想法:(monthcalGergorian是我班级的属性,但应该是自我解释的)

dcNow

(原谅我的匈牙利表示法)

要解决您的问题,您基本上会在if语句中将 _dcNextWeekReminderTime = [[NSDateComponents alloc] init]; _dcNextWeekReminderTime.weekday = 6; _dcNextWeekReminderTime.weekOfYear = self.dcNow.weekOfYear; _dcNextWeekReminderTime.year = self.dcNow.year; _dcNextWeekReminderTime.calendar = self.calGregorian; //if the date we've just created is earlier than today... if ([[self.calGregorian dateFromComponents:_dcNextWeekReminderTime] timeIntervalSinceDate:[self.calGregorian dateFromComponents:self.dcNow]] < 0) { //...make it next week _dcNextWeekReminderTime.weekOfYear +=1; } 更改为>,并将<更改为+=1,将数字更改为6(星期五)到2(星期一)。

此外,除非您使用旧的iOS版本,否则-=1似乎已被弃用,而不是week