【问题标题】:How to get first and last date of a given week number and year?如何获取给定周数和年份的第一个和最后一个日期?
【发布时间】:2014-04-27 22:36:04
【问题描述】:

我是 ios 编程新手,我拼命想用参考年份获取一周的第一个和最后一个日期。

我尝试构建一个方法,但确实得到了奇怪的日期。

+ (NSArray*)getStartDateOfWeek : (NSInteger)weekNumber withYear:(NSInteger)year {
    NSMutableArray *result = [[NSMutableArray alloc]init];

    // Week Start Date

    NSCalendar *gregorianStart = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *componentsStart = [[NSDateComponents alloc] init];


    [componentsStart setWeekOfYear:weekNumber];
    [componentsStart setYear:year];

    int startDayOfWeek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[gregorianStart dateFromComponents:componentsStart]] weekday];
    [componentsStart setDay:([componentsStart day] - ((startDayOfWeek) - 2))];

    NSDate *beginningOfWeek = [gregorianStart dateFromComponents:componentsStart];

    // Week End Date

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

    NSDateComponents *componentsEnd = [[NSDateComponents alloc]init];
    [componentsStart setWeekOfYear:weekNumber];
    [componentsStart setYear:year];

    int endDayOfWeek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[gregorianEnd dateFromComponents:componentsEnd]] weekday];

    [componentsEnd setDay:([componentsEnd day]+ (7 - endDayOfWeek) + 1)]; // for end day of the week

    NSDate *endOfWeek = [gregorianEnd dateFromComponents:componentsEnd];

    [result insertObject:beginningOfWeek atIndex:0];
    [result insertObject:endOfWeek atIndex:1];

    return result;
}

感谢任何帮助。

【问题讨论】:

    标签: ios objective-c nsdate


    【解决方案1】:

    假设您想要给定一周的星期日(第一天)和星期六(最后一天),此代码将执行您想要的操作(为清楚起见,我创建了两种方法,但您可以将它们组合回一个) -

    + (NSDate *)firstDayOfWeek:(NSInteger)weekNumber inYear:(NSInteger)year
    {
        NSDateComponents *comps=[[NSDateComponents alloc]init];
        [comps setWeekday:1];                                   //Change this to 2 if you want Monday rather than Sunday
        [comps setWeekOfYear:weekNumber];
    
        [comps setYear:year];
        [comps setYearForWeekOfYear:year];
    
        NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDate *newDate=[cal dateFromComponents:comps];
    
        return newDate;
    
    }
    
    + (NSDate *)lastDayOfWeek:(NSInteger)weekNumber inYear:(NSInteger)year
    {
        NSDateComponents *comps=[[NSDateComponents alloc]init];
        [comps setWeekday:7];                                  //Change this to 6 if you want Friday rather than Saturday
        [comps setWeekOfYear:weekNumber];
    
        [comps setYear:year];
        [comps setYearForWeekOfYear:year];
    
        NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDate *newDate=[cal dateFromComponents:comps];
    
        return newDate;
    
    }
    

    一个重要的点是[comps setYearForWeekOfYear:year]; - 否则您可能会在第 1 周得到意想不到的结果 - 默认情况下,您将获得一年中的“最后”第 1 周,这也是下一年的第 1 周,因此您结束比你预期的晚一年。

    如果您想将两者合二为一,我会使用NSDictionary 而不是NSArray -

     +(NSDictionary *)firstAndLastDayOfWeek:(NSInteger)weekNumber inYear:(NSInteger)year
        {
            NSDateComponents *comps=[[NSDateComponents alloc]init];
            [comps setWeekday:1];
            [comps setWeekOfYear:weekNumber];
    
            [comps setYear:year];
            [comps setYearForWeekOfYear:year];
    
            NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
            NSDate *firstDay=[cal dateFromComponents:comps];
    
            [comps setWeekday:7];
    
            NSDate *lastDay=[cal dateFromComponents:comps];
    
            return @{@"firstDay":firstDay,@"lastDay":lastDay};
    
        }
    

    【讨论】:

    • 非常感谢您的解释。 NSDate + NSCalendar 是 Objective-c 中令人困惑的部分。它有效...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2021-12-07
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多