我是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;
}
非常感谢任何帮助。
答案 0 :(得分:2)
假设您想要给定周的星期日(第一天)和星期六(最后一天),此代码将执行您想要的操作(为清楚起见,我创建了两种方法,但您可以将它们组合成一种方法) -
+ (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};
}