簡體   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