下周计算

时间:2014-08-26 13:47:53

标签: ios objective-c datetime nsdate nsdatecomponents

我有一个要求,我必须计算工作日,即从星期一到星期五。如果我今天通过特定周开始日期(星期一和星期五)和结束日期(星期五的日期)将会显示。我使用以下代码计算。

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

   [dateFormat setDateFormat:@"yyyy-MM-dd"];// you can use your format.
    todayDate= [NSDate date];

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:todayDate];

    int dayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:todayDate] weekday];// this will give you current day of week

    [components setDay:([components day] - ((dayofweek) - 2))];// for beginning of the week.

    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];

    [dateFormat_first setDateFormat:@"yyyy-MM-dd"];
    NSString * dateString2Prev = [dateFormat stringFromDate:beginningOfWeek];
    NSDate * weekstartPrev = [dateFormat_first dateFromString:dateString2Prev];
   // NSLog(@"The start of the week %@",weekstartPrev);
    startDate= [weekstartPrev dateByAddingTimeInterval:60*60*24*1];
    //NSLog(@"The date plus one is %@", startDate);
    //Week End Date


    NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:todayDate];
    int Enddayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:todayDate] weekday];// this will give you current day of week
    [componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week
    NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd];
    NSDateFormatter *dateFormat_End = [[NSDateFormatter alloc] init];
    [dateFormat_End setDateFormat:@"yyyy-MM-dd"];
    NSString *dateEndPrev = [dateFormat stringFromDate:EndOfWeek];
    NSDate *weekEndPrev = [dateFormat_End dateFromString:dateEndPrev];


    endDate= [weekEndPrev dateByAddingTimeInterval: -86400.0];
    NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
    [dateF setDateFormat:@"dd-MMM-yyyy"];

    [dateF setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateF setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];

    endDateString = [dateF stringFromDate:endDate];      //pass this date to time table view controller

    NSLog(@"Start date %@ and End date %@ ",startDate, endDate);

    NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
    [dateF setDateFormat:@"dd-MMM-yyyy"];

    [dateF setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateF setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];

    startDateString = [dateF stringFromDate:startDate];   
    NSLog(@"the date start %@ ", startDateString);

现在我的问题是如何计算下周,例如今天的日期 2014年8月26日星期二。我使用上面的代码计算了本周,即 2014年8月25日星期一至2014年8月29日星期五。我如何计算下周,即2014年9月1日(星期一)至2014年9月5日(星期五)

1 个答案:

答案 0 :(得分:2)

我几周前就处理过类似问题。

我在NSDate上创建了一个类别。您可以使用这些方法。

- (NSDate *)firstDayOfWeek
{
    return [NSDate firstDayOfWeekFor:self];
}

+(NSDate *)firstDayOfWeekFor:(NSDate*)p_Date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:p_Date];
    NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
    if ([weekdayComponents weekday] == 1)
    {
        // dimanche, on soustrait 6 jours
        [componentsToSubtract setDay:-6];
    }
    else
    {
        // autres jours, on soustrait le jour courant et on rajoute 2 (=lundi)
        [componentsToSubtract setDay:(-[weekdayComponents weekday] + 2)];
    }
    return [calendar dateByAddingComponents:componentsToSubtract toDate:[p_Date withoutTime] options:0];
}

- (NSDate *) dateByAddingDays:(NSInteger)p_Days
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:p_Days];
    return [calendar dateByAddingComponents:components toDate:self options:0];
}