Search code examples
swift3calendarnsdatenscalendar

How to set the first date of current week to Wednesday in Swift?


How to set the first date of the week to Wednesday and show the first and last date of week on yellow bar? Because the schedule will update every Wednesday.

App design screenshot


Solution

  • Please check this might help :

    var calendar = Calendar.current
    calendar.firstWeekday = 4
    
    let startOfWeek = calendar.date(from: calendar.dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date()))!    
    let endOfWeek = calendar.date(byAdding: .day, value: 6, to: startOfWeek)!
    
    print(startOfWeek)
    print(endOfWeek)