簡體   English   中英

NSDate獲取將來的下一個工作日

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM