【问题标题】:Next Week Calculation下周计算
【发布时间】:2014-08-26 13:47:53
【问题描述】:

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

 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 日(星期五)

【问题讨论】:

    标签: ios objective-c datetime nsdate nsdatecomponents


    【解决方案1】:

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

    我在 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];
    }
    

    【讨论】:

    • 我使用了你的 dateByAdding days 方法。太棒了..刚开始日期(星期一)增加了 7 天,结束日期(星期五)也增加了 7 天
    猜你喜欢
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2016-09-15
    • 1970-01-01
    相关资源
    最近更新 更多