繁体   English   中英

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

[英]Get the next weekday of Date()

我的应用程序像这样被分解了,

  1. 用户选择 startDate 和 endDate
  2. 然后选择他们想要创建重复计费的 billDate (1st,14th,28th)
  3. 现在我正在尝试使用给定的日期创建一个 billStartDate 和 billEndDate。

例如,如果用户选择 01/11/2020 作为开始,选择 01/03/2021 作为结束,那么账单日期是 14 日,我正在尝试为 14/11/2020 创建 billStartDate 和 billEndDate 14 /02/2021。

像下面这样的东西,

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
}

}

以上给了我以下错误

键入“()-> Int?” 不能符合“BinaryInteger”; 只有结构/枚举/类类型可以符合协议

改变

let day = start.dayNumberOfWeek

let day = start.dayNumberOfWeek()

甚至更好

if let day = start.dayNumberOfWeek() {

(仍然会有编译错误,但这应该足以让你再次移动。)

暂无
暂无

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

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