Calendar days condition

Viewed 59

In the application I need to set the condition that on a certain day of the week one variable will be true. For example

if Calendar.day == monday {
 var mon = true
}

I know that this is not right. But for now I do not quite understand how days of the week are. Please help me with this

1 Answers

Try out this. The dayInWeek string will present the current day of the week such as "Monday", or "Tuesday". The switch statement will check for what day it is, and you can perform a function when it is that subsequent day.

let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let dayInWeek = dateFormatter.string(from: date)

    switch dayInWeek {
    case "Monday":
        print("Today is Monday")

    case "Tuesday":
        print("Today is Tuesday")


    default:
       break
   }

Of course, you'll finish it off for the rest of the days in the week.

Related