Menu App in Swift UI - Show a Specific View according to the day and h of the week.

I need is to show the main menu Monday to Friday and Saturday and Sunday a brunch menu. So I slip the view in two menus and I want to create a if statement that check-in which day and hours of the day we are and show the preselected menu.
Answered by BabyJ in 626895022
Here’s a simple switch statement that will check the day.

Code Block Swift
let dayOfWeek = Calendar.current.component(.weekday, from: Date())
let hour = Calendar.current.component(.hour, from: Date())
switch dayOfWeek {
case 2...6:
// weekday menu
case 1, 7:
// weekend menu
default:
print("Unknown day")
}

Use the hour variable to check the hour:
Code Block Swift
if hour < 12 {
// morning
} else {
// afternoon
}

or you can use another switch statement here for specific times.
Accepted Answer
Here’s a simple switch statement that will check the day.

Code Block Swift
let dayOfWeek = Calendar.current.component(.weekday, from: Date())
let hour = Calendar.current.component(.hour, from: Date())
switch dayOfWeek {
case 2...6:
// weekday menu
case 1, 7:
// weekend menu
default:
print("Unknown day")
}

Use the hour variable to check the hour:
Code Block Swift
if hour < 12 {
// morning
} else {
// afternoon
}

or you can use another switch statement here for specific times.
Hi BabyJ, thank you so Much for your help. I did not think about the Switch Statement - I just started study and There is a lot to leant. I much appreciated!!

I will apply the code and I will let you know!

All the best

Nico
Menu App in Swift UI - Show a Specific View according to the day and h of the week.
 
 
Q