简体   繁体   English

获取 Date() 的下一个工作日

[英]Get the next weekday of Date()

My app is broken down like so,我的应用程序像这样被分解了,

  1. The user selects a startDate and endDate用户选择 startDate 和 endDate
  2. Then chooses the billDate which they want to create reoccurring billing (1st,14th,28th)然后选择他们想要创建重复计费的 billDate (1st,14th,28th)
  3. Now I am trying to create a billStartDate and billEndDate using the dates given.现在我正在尝试使用给定的日期创建一个 billStartDate 和 billEndDate。

For example, if the user selects 01/11/2020 as the start and 01/03/2021 as the end, then a bill date is the 14th, I am trying to create a billStartDate for 14/11/2020 and billEndDate the 14/02/2021.例如,如果用户选择 01/11/2020 作为开始,选择 01/03/2021 作为结束,那么账单日期是 14 日,我正在尝试为 14/11/2020 创建 billStartDate 和 billEndDate 14 /02/2021。

Something like the following,像下面这样的东西,

extension Date {

func setBillStartDate(start: Date, billdate: Payment.BillDate) -> Date {
    
    let day = start.dayNumberOfWeek
    var startDate = Date()
    
    switch billdate {
    
    case .start:
        
        if day > 1 {
            // Create a start date on the next 1st

        } else if day == 1 {
           // is the first then on that day
            startDate = start
        }
                
    case .middle:
        
        if day > 1 {
            // Create a date on the next 14th

        } else if day == 14 {
           // if is the 14th then on that day
            startDate = start
        }
                
    case .end:
        
        if day > 1 {
            // Create a date on the next 28th

        } else if day == 28 {
           // if is the 14th then on that day
            startDate = start
        }
                
    }
    
    return startDate
    
}

func setBillEndDate(date: Date, billdate: Payment.BillDate) -> Date {
    
    let day = date.dayNumberOfWeek
    var endDate = Date()
    
    switch billdate {
    
    case .start:
        
        if day > 1 {
            // Create a start date on the next 1st

        } else if day == 1 {
           // is the first then on that day
            endDate = date
        }
                
    case .middle:
        
        if day > 1 {
            // Create a date on the next 14th
            
        } else if day == 14 {
           // if is the 14th then on that day
            endDate = date
        }
                
    case .end:
        
        if day > 1 {
            // Create a date on the next 28th

        } else if day == 28 {
           // if is the 14th then on that day
            endDate = date
        }
                
    }
    
    return endDate
    
}

func dayNumberOfWeek() -> Int? {
    return Calendar.current.dateComponents([.weekday], from: self).weekday
}

}

The above gives me the following error以上给了我以下错误

Type '() -> Int?'键入“()-> Int?” cannot conform to 'BinaryInteger';不能符合“BinaryInteger”; only struct/enum/class types can conform to protocols只有结构/枚举/类类型可以符合协议

Change改变

let day = start.dayNumberOfWeek

To

let day = start.dayNumberOfWeek()

Or even better甚至更好

if let day = start.dayNumberOfWeek() {

(There will still be compile errors, but that should be enough to get you moving again.) (仍然会有编译错误,但这应该足以让你再次移动。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM