gpt4 book ai didi

ios - 如何获取给定周数和年份的第一个和最后一个日期?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:02 25 4
gpt4 key购买 nike

我是 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;
}

非常感谢任何帮助。

最佳答案

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

+ (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};

}

关于ios - 如何获取给定周数和年份的第一个和最后一个日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23329893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com