我是 ios 编程新手,我拼命想用引用年份获取一周的第一个和最后一个日期。
我尝试构建一个方法,但确实得到了奇怪的日期。
+ (NSArray*)getStartDateOfWeek : (NSInteger)weekNumber withYearNSInteger)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;
}
感谢任何帮助。
Best Answer-推荐答案 strong>
假设您想要给定一周的星期日(第一天)和星期六(最后一天),此代码将执行您想要的操作(为清楚起见,我创建了两种方法,但您可以将它们组合回一个) -
+ (NSDate *)firstDayOfWeekNSInteger)weekNumber inYearNSInteger)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 *)lastDayOfWeekNSInteger)weekNumber inYearNSInteger)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 *)firstAndLastDayOfWeekNSInteger)weekNumber inYearNSInteger)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/
|