[英]NSDate get next occurring weekday that is in the future
解决我的问题的逻辑是满口的,我已经想出了一个解决方案。 我的解决方案发布在下面。 我正在寻找是否有人可以提出更有效/更简单的解决方案。
该方法应该返回给定日期的下一个未来(从现在开始)发生的日期。 在输入日期和输出日期之间应保留时间。
对于所有示例,今天是2015年5月8日(星期五)下午4:00:所有投入和产出均在2015年:
+---------------------------+---------------------------+
| Input | Output |
+---------------------------+---------------------------+
| Tuesday April 7, 3:00 PM | Monday May 11, 3:00 PM |
| Thursday May 7, 3:00 PM | Monday May 11, 3:00 PM |
| Thursday May 7, 5:00 PM | Friday May 8, 5:00 PM |
| Tuesday May 12, 3:00 PM | Wednesday May 13, 3:00 PM |
| Saturday June 20, 3:00 PM | Monday June 22, 3:00 PM |
+---------------------------+---------------------------+
这是逻辑的一些伪代码:
do {
date += 1 day;
} while(date.isWeekend || date.isInThePast)
这是我想出的解决方案,避免使用循环来牢记效率:
- (NSDate *)nextWeekDayInFuture {
NSDate *now = [NSDate date];
NSDate *nextWeekDaydate;
NSCalendar *calendar = [NSCalendar currentCalendar];
nextWeekDaydate = [self dateByAddingDays:1]; // custom method, adds 1 day
if ([nextWeekDaydate isLessThan:now]) {
NSDateComponents *nowComponents = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:now];
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:nextWeekDaydate];
[nowComponents setHour:dateComponents.hour];
[nowComponents setMinute:dateComponents.minute];
[nowComponents setSecond:dateComponents.second];
nextWeekDaydate = [calendar dateFromComponents:nowComponents];
if ([nextWeekDaydate isLessThan:now]) {
nextWeekDaydate = [nextWeekDaydate dateByAddingDays:1];
}
}
NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:nextWeekDaydate];
if (components.weekday == Saturday) {
nextWeekDaydate = [nextWeekDaydate dateByAddingDays:2];
} else if (components.weekday == Sunday) {
nextWeekDaydate = [nextWeekDaydate dateByAddingDays:1];
}
return nextWeekDaydate;
}
发布解决方案之前,请使用上方的输入/输出表测试您的逻辑。
没有理由不将逻辑置于循环中,因为至多它会循环两次(如果当前日期是星期六)。
如果当前一天是周末,这是解决问题的解决方案: 如何使用NSDate从今天开始查找工作日?
- (NSInteger)isWeekend:(NSDate*)inDate
{
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:kCFCalendarUnitWeekday fromDate:inDate];
return [comp weekday];
}
然后,我们可以调用此方法,检查返回的NSInteger以查看它是坐着还是晒太阳,然后再次运行它:
NSDate *now = [NSDate date];
now = [now dateByAddingTimeInterval:60*60*24]; // Add 1 day's worth.
NSInteger curDay = [self isWeekend:now];
while ((curDay == 6) || (curDay == 1)) // Sat or Sund.
{
now = [now dateByAddingTimeInterval:60*60*24]; // Add 1 day's worth.
curDay = [self isWeekend:now];
}
如果您确实想删除该循环,则可以检查它是否是星期六并增加了2天的价值,或者如果是星期日并又增加了1天的价值。
NSDate *now = [NSDate date];
now = [now dateByAddingTimeInterval:60*60*24]; // Add 1 day's worth.
NSInteger curDay = [self isWeekend:now];
if (curDay == 6) // Sat
{
now = [now dateByAddingTimeInterval:60*60*24*2]; // Add 2 day's worth.
}
else if (curDay == 1) // Sun
{
now = [now dateByAddingTimeInterval:60*60*24]; // Add 1 day's worth.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.