gpt4 book ai didi

ios - 如何获得下个月的第1、2、3、4个工作日(工作日)

转载 作者:行者123 更新时间:2023-12-01 18:14:39 30 4
gpt4 key购买 nike

我一直在寻找下个月第一,第二,第三或第四工作日(工作日)的日期。

我有以下代码:

NSTimeInterval *lastDue; // unix time stamp from last due date
NSDate *ldue=[NSDate dateWithTimeIntervalSince1970:lastDue];
NSDateComponents *dc =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit|NSWeekdayOrdinalCalendarUnit) fromDate:ldue];

NSLog(@"firstWeekday: %ld",(long)dc.weekday);

任何人都有从哪里开始的提示?

谢谢

最佳答案

我认为这可以解决您的问题:

    // Get the date of today
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
// Set the next month, you don't have t worry in December, it will automatically switch to the next year
components.month = components.month+1;
// Iterate over the first four days of the month
for(int i=1;i<5;i++)
{
components.day = i;
// Get the new date
NSDate *dayInMonth = [gregorian dateFromComponents:components];
// Get the day in the week
NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:dayInMonth];
NSInteger weekday = [weekdayComponents weekday];
// Sunday = 1, Monday = 2, ...
BOOL isWeekDay = YES;
if(weekday==7||weekday==1) {
isWeekDay = NO;
}
// Format properly the output
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yy - hh:mm a"];
NSString *dateWithNewFormat = [formatter stringFromDate:dayInMonth];
// Print the date and 1 if is a weekday or 0 if not
NSLog(@"'%@': %d",dateWithNewFormat, isWeekDay);
}

关于ios - 如何获得下个月的第1、2、3、4个工作日(工作日),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23385521/

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