gpt4 book ai didi

ios - 下周计算

转载 作者:行者123 更新时间:2023-11-28 21:59:18 25 4
gpt4 key购买 nike

我有一个要求,我必须计算工作日,即从星期一到星期五。如果我通过今天的日期,将显示特定周的开始日期(星期一的日期)和结束日期(星期五的日期)。我使用下面的代码计算。

 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 日(星期五)

最佳答案

几周前我已经处理过类似的问题。

我在 NSDate 上创建了一个类别。您可以使用这些方法。

- (NSDate *)firstDayOfWeek
{
return [NSDate firstDayOfWeekFor:self];
}

+(NSDate *)firstDayOfWeekFor:(NSDate*)p_Date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:p_Date];
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
if ([weekdayComponents weekday] == 1)
{
// dimanche, on soustrait 6 jours
[componentsToSubtract setDay:-6];
}
else
{
// autres jours, on soustrait le jour courant et on rajoute 2 (=lundi)
[componentsToSubtract setDay:(-[weekdayComponents weekday] + 2)];
}
return [calendar dateByAddingComponents:componentsToSubtract toDate:[p_Date withoutTime] options:0];
}

- (NSDate *) dateByAddingDays:(NSInteger)p_Days
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:p_Days];
return [calendar dateByAddingComponents:components toDate:self options:0];
}

关于ios - 下周计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25507451/

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