【发布时间】:2014-08-26 13:47:53
【问题描述】:
我有一个要求,我必须计算工作日,即从星期一到星期五。如果我通过今天的日期,将显示特定的一周开始日期(星期一的日期)和结束日期(星期五的日期)。我使用下面的代码计算。
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];// you can use your format.
todayDate= [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:todayDate];
int dayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:todayDate] weekday];// this will give you current day of week
[components setDay:([components day] - ((dayofweek) - 2))];// for beginning of the week.
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:@"yyyy-MM-dd"];
NSString * dateString2Prev = [dateFormat stringFromDate:beginningOfWeek];
NSDate * weekstartPrev = [dateFormat_first dateFromString:dateString2Prev];
// NSLog(@"The start of the week %@",weekstartPrev);
startDate= [weekstartPrev dateByAddingTimeInterval:60*60*24*1];
//NSLog(@"The date plus one is %@", startDate);
//Week End Date
NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:todayDate];
int Enddayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:todayDate] weekday];// this will give you current day of week
[componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+1)];// for end day of the week
NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd];
NSDateFormatter *dateFormat_End = [[NSDateFormatter alloc] init];
[dateFormat_End setDateFormat:@"yyyy-MM-dd"];
NSString *dateEndPrev = [dateFormat stringFromDate:EndOfWeek];
NSDate *weekEndPrev = [dateFormat_End dateFromString:dateEndPrev];
endDate= [weekEndPrev dateByAddingTimeInterval: -86400.0];
NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
[dateF setDateFormat:@"dd-MMM-yyyy"];
[dateF setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateF setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
endDateString = [dateF stringFromDate:endDate]; //pass this date to time table view controller
NSLog(@"Start date %@ and End date %@ ",startDate, endDate);
NSDateFormatter *dateF = [[NSDateFormatter alloc] init];
[dateF setDateFormat:@"dd-MMM-yyyy"];
[dateF setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateF setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
startDateString = [dateF stringFromDate:startDate];
NSLog(@"the date start %@ ", startDateString);
现在我的问题是如何计算下周,例如今天的日期是 2014 年 8 月 26 日星期二。我使用上面的代码计算了当前的一周,即从 2014 年 8 月 25 日星期一到 2014 年 8 月 29 日星期五。如何计算下周,即从 2014 年 9 月 1 日(星期一)到 2014 年 9 月 5 日(星期五)。
【问题讨论】:
标签: ios objective-c datetime nsdate nsdatecomponents