2015-11-22 4 views
0

Im изготовление работ праздник/резервирование дня на день. У меня есть расширение, в котором работает общее количество дней между двумя NSDates, оно отлично работает, но я не знаю, как удалить все выходные дни из общей суммы.NSdate диапазон полного удаления выходных дней Swift

Вы можете помочь с этим!

extension NSDate { 
    func numberOfDaysUntilDateTime(toDateTime: NSDate, inTimeZone  timeZone: NSTimeZone? = nil) -> Int { 
     let calendar = NSCalendar.currentCalendar() 
     if let timeZone = timeZone { 
      calendar.timeZone = timeZone 
     } 
     var startDate: NSDate?, endDate: NSDate? 
     calendar.rangeOfUnit(.Day, startDate: &startDate, interval: nil, forDate: self) 
     calendar.rangeOfUnit(.Day, startDate: &endDate, interval: nil, forDate: toDateTime) 
     let difference = calendar.components(.Day, fromDate: startDate!, toDate: endDate!, options: []) 
     return difference.day 
    } 
} 

ответ

3

Я нашел это. Он определяет количество дней выходных дней между 2 NSDates

func numberOfWeekendsBeetweenDates(startDate startDate:NSDate,endDate:NSDate)->Int{ 
    var count = 0 
    let oneDay = NSDateComponents() 
    oneDay.day = 1; 
    // Using a Gregorian calendar. 
    let calendar = NSCalendar.currentCalendar() 
    var currentDate = startDate; 
    // Iterate from fromDate until toDate 
    while (currentDate.compare(endDate) != .OrderedDescending) { 
     let dateComponents = calendar.components(.Weekday, fromDate: currentDate) 
     if (dateComponents.weekday == 1 || dateComponents.weekday == 7) { 
      count++; 
     } 
     // "Increment" currentDate by one day. 
     currentDate = calendar.dateByAddingComponents(oneDay, toDate: currentDate, options: [])! 
    } 
    return count 
} 
Смежные вопросы