gpt4 book ai didi

ios - 在 objective-c 中仅从 NSDate 中选择工作日

转载 作者:行者123 更新时间:2023-11-29 00:58:47 25 4
gpt4 key购买 nike

我担心的是..如果我有包含开始日期的文本字段。当我从日历中选择日期为星期五时..然后我想在结束日期文本字段中跳过周末周六和周日...我尝试过这个

NSString *strCurrentDate;
NSString *strNewDate;

NSDate *date = [NSDate date];

NSDateFormatter *df =[[NSDateFormatter alloc] init];

[df setDateFormat:@"yyyy-MM-dd"];
[df setTimeStyle:NSDateFormatterMediumStyle];

strCurrentDate = [df stringFromDate:date];
NSLog(@"Current Date and Time: %@",strCurrentDate);

int hoursToAdd = roundedUp;

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:hoursToAdd];

NSDate *newDate= [calendar dateByAddingComponents:components toDate:date options:0];

[df setDateFormat:@"yyyy-MM-dd"];
[df setTimeStyle:NSDateFormatterMediumStyle];

strNewDate = [df stringFromDate:newDate];

NSLog(@"New Date and Time: %@",strNewDate);

while ([strNewDate compare:strCurrentDate] == NSOrderedAscending)
{
NSDateComponents *dateComponents = [calendar components:NSWeekdayCalendarUnit fromDate:strNewDate];

if (dateComponents.weekday == friday)
{
count = count+2;
NSLog(@"count = %d", count);
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:2];
NSString *endDate = [strNewDate dateByAddingTimeInterval:2*60*60*24];
// [oneDay setDay:2];
}
}

但是我不仅仅只有工作日......所以有人可以做必要的事情......提前谢谢你:)

最佳答案

要获取从现在开始的下一个工作日,请使用 NSCalendar 的方法 isDateInWeekend

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Strip the time portion of the date
NSDate *date = [calendar startOfDayForDate:[NSDate date]];
do {
// add 1 day until the result is not in a weekend
date = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:date options:NSCalendarMatchNextTime];
} while (![calendar isDateInWeekend:date]);
NSLog(@"Next Workday: %@", date);

关于ios - 在 objective-c 中仅从 NSDate 中选择工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37290469/

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