【问题标题】:How to get next week start and end date in Objective-c?如何在Objective-c中获取下周的开始和结束日期?
【发布时间】:2018-05-31 10:23:04
【问题描述】:

我试图获取下周的开始和结束日期,但我得到了当前周的开始和结束日期。如何在 Objective-c 中获取下周的开始和结束日期。

【问题讨论】:

  • 显示您尝试过的内容?

标签: objective-c


【解决方案1】:

NSCalendar 包含执行此操作的专用方法,例如 nextDateAfterDate:matchingUnit:value:options:dateByAddingComponents:toDate:options:

// Get the current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
// Get the next occurrence for the first weekday of the current calendar
NSDate *startOfNextWeek = [calendar nextDateAfterDate:[NSDate date] matchingUnit:NSCalendarUnitWeekday value:calendar.firstWeekday options:NSCalendarMatchStrictly];
// Create new date components +7 days and -1 seconds
NSDateComponents *endOfNextWeekComponents = [[NSDateComponents alloc] init];
endOfNextWeekComponents.day = 7;
endOfNextWeekComponents.second = -1;
// Add the date components to the start date to get the end date.
NSDate *endOfNextWeek = [calendar dateByAddingComponents:endOfNextWeekComponents toDate:startOfNextWeek options:NSCalendarMatchStrictly];

NSLog(@"%@ - %@", startOfNextWeek, endOfNextWeek);

【讨论】:

    【解决方案2】:

    您可以使用以下代码获取本周和下周的所有日期:-

    NSArray * allDatesOfThisWeek = [self daysThisWeek];
    NSArray * allDatesOfNextWeek = [self daysNextWeek];
    

    以下方法用于计算本周日期:-

    -(NSArray*)daysThisWeek
    {
         return  [self daysInWeek:0 fromDate:[NSDate date]];
    }
    
    -(NSArray*)daysNextWeek
    {
        return  [self daysInWeek:1 fromDate:[NSDate date]];
    }
    -(NSArray*)daysInWeek:(int)weekOffset fromDate:(NSDate*)date
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        //ask for current week
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:date];
        //create date on week start
        NSDate* weekstart=[calendar dateFromComponents:comps];
    
        NSDateComponents* moveWeeks=[[NSDateComponents alloc] init];
        moveWeeks.weekOfYear=weekOffset;            
        weekstart=[calendar dateByAddingComponents:moveWeeks toDate:weekstart options:0];
    
    
        //add 7 days
        NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
        for (int i=1; i<=7; i++) {
            NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
            compsToAdd.day=i;
            NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
            [week addObject:nextDate];
    
        }
        return [NSArray arrayWithArray:week];
    }
    

    如果您想从今天开始获取下周的日期,则像这样传递 weekOffset=2:-

    NSArray * allDatesOfNextToNextWeek = [self daysInWeek:2 fromDate:now];
    

    如果您想从今天开始获取前一周的日期,请像这样传递 weekOffset=-1:-

    NSArray * allDatesOfPreviousWeek = [self daysInWeek:-1 fromDate:now];
    

    希望,这就是您要找的。有任何问题可以联系我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2021-05-19
      相关资源
      最近更新 更多