简体   繁体   中英

Get the next weekday of Date()

My app is broken down like so,

  1. The user selects a startDate and endDate
  2. Then chooses the billDate which they want to create reoccurring billing (1st,14th,28th)
  3. Now I am trying to create a billStartDate and billEndDate using the dates given.

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.

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?' cannot conform to '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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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