考虑到只将选定的一天计入工作日,找到最终日期的逻辑是什么?
What is the logic to find final date considering only chosen day are to be counted in a weekday?
假设某些任务只能在周一、周三、周六和周日(每周 4 天)完成。
如果我在日期添加任务,即 11 月 3 日,星期二,并提到我将在 8 天内完成任务。考虑到任务开始日是星期二,星期一已经过去了,所以我这周只剩下三天了,在8天后(任务完成日)找到那一天和日期的逻辑是什么。
我需要这个逻辑来编写 Objective C 代码。
根据我对您的要求的理解,我用 swift 在 PlayGround 中写了一些代码。它可能对你有帮助。 (你只需要在Objective-C中转换它)
let df = NSDateFormatter()
df.dateFormat = "dd-MM-yyyy"
let dateToCheck = df.dateFromString("8-11-2015")
let comp = NSCalendar.currentCalendar().components(NSCalendarUnit.Weekday, fromDate: dateToCheck!)
// Monday == 2 , Wednesday == 4, Saturday = 7 and Sunday = 1
switch(comp.weekday) {
case 2,3:
print("3 Day Left")
case 4,5,6:
print("2 Day Left")
case 7:
print("1 Day Left")
case 1:
print("Last Day Left")
default:
print("")
}
我刚刚使用了日期组件的工作日。
您好,我将上面的代码转换为 Objective-C。
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = @"dd-MM-yyyy";
NSDate *dateToCheck = [df dateFromString:@"8-11-2015"];
NSDateComponents *comp = [[NSCalendar currentCalendar]components:NSCalendarUnitWeekday fromDate:dateToCheck];
// Monday == 2 , Wednesday == 4, Saturday = 7 and Sunday = 8
if (comp.weekday == 2 || comp.weekday == 3) {
NSLog(@"3 Day Left");
}else if (comp.weekday == 4 || comp.weekday == 5 || comp.weekday == 6){
NSLog(@"2 Day Left");
}else if (comp.weekday == 7 || comp.weekday == 1){
NSLog(@"1 Day Left");
}else if (comp.weekday == 1 ){
NSLog(@"Last Day Left");
}
假设某些任务只能在周一、周三、周六和周日(每周 4 天)完成。
如果我在日期添加任务,即 11 月 3 日,星期二,并提到我将在 8 天内完成任务。考虑到任务开始日是星期二,星期一已经过去了,所以我这周只剩下三天了,在8天后(任务完成日)找到那一天和日期的逻辑是什么。
我需要这个逻辑来编写 Objective C 代码。
根据我对您的要求的理解,我用 swift 在 PlayGround 中写了一些代码。它可能对你有帮助。 (你只需要在Objective-C中转换它)
let df = NSDateFormatter()
df.dateFormat = "dd-MM-yyyy"
let dateToCheck = df.dateFromString("8-11-2015")
let comp = NSCalendar.currentCalendar().components(NSCalendarUnit.Weekday, fromDate: dateToCheck!)
// Monday == 2 , Wednesday == 4, Saturday = 7 and Sunday = 1
switch(comp.weekday) {
case 2,3:
print("3 Day Left")
case 4,5,6:
print("2 Day Left")
case 7:
print("1 Day Left")
case 1:
print("Last Day Left")
default:
print("")
}
我刚刚使用了日期组件的工作日。
您好,我将上面的代码转换为 Objective-C。
NSDateFormatter *df = [[NSDateFormatter alloc]init];
df.dateFormat = @"dd-MM-yyyy";
NSDate *dateToCheck = [df dateFromString:@"8-11-2015"];
NSDateComponents *comp = [[NSCalendar currentCalendar]components:NSCalendarUnitWeekday fromDate:dateToCheck];
// Monday == 2 , Wednesday == 4, Saturday = 7 and Sunday = 8
if (comp.weekday == 2 || comp.weekday == 3) {
NSLog(@"3 Day Left");
}else if (comp.weekday == 4 || comp.weekday == 5 || comp.weekday == 6){
NSLog(@"2 Day Left");
}else if (comp.weekday == 7 || comp.weekday == 1){
NSLog(@"1 Day Left");
}else if (comp.weekday == 1 ){
NSLog(@"Last Day Left");
}