How to perform an action based on the local time?

Hi! I would like to make text appear based on the local time of the user. The idea is that when it's between 00:00:01 AM and 11:00:00 AM, that the text will say "breakfast" and that when it's between 11:00:00 AM and 03:00:00 PM, the text will say "lunch". Etc.

I found this code online but I can't get it working.

Can you help me? Possibly with other code?

.

Contentview:

struct Test: View {
    
    var now = Date()
    var breakfastTime = Date.parse("00:00:01")
    var lunchTime = Date.parse("11:00:00")
    var dinerTime = Date.parse("16:00:00")
    
    func activeDinerCourse() -> String {
        
        if now > breakfastTime && now < lunchTime {
            return "Breakfast"
        }
        if now > lunchTime && now < dinerTime {
            return "Lunch"
        }
        
        if now > dinerTime && now < Date.parse("23:59:59"){
            return "Diner"
        }
        
        return "Something went wrong..."
        
    }
    
    
    var date = Date.now.formatted(date: .omitted, time: .shortened)
    
    var body: some View {
        
        VStack {
            Text("\(date)")
            
            Text(activeDinerCourse())
            
        }
        
    }
}

.

.Parse extention

public extension Date {

    static func parse(_ string: String, format: String = "HH:mm:ss") -> Date {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = NSTimeZone.default
        dateFormatter.dateFormat = format

        let date = dateFormatter.date(from: string)!
        return date
    }

    func dateString(_ format: String = "HH:mm:ss") -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: self)
    }
}

.

Thank you!

Accepted Reply

Your code doesn't work because of the way dates are created. Through your extension on Date, you are creating one from a string in the format of "HH:mm:ss" (hours, minutes, seconds). That is all the data the date has in order to be created so other values, such as day, month, year, are filled in from the reference date (00:00:00 UTC on 1 January 2001). In your case, the breakfast date is "01/01/2000 00:00:01". However, the now date has other date components (day, month, year…) provided and so won't be between two dates 23 years ago.

With the code you have, you can just change the way you create the now variable like this:

var now = Date.parse(Date().dateString())

This extracts the provided components in the format string from the current date using your extensions on Date.

Replies

Your code doesn't work because of the way dates are created. Through your extension on Date, you are creating one from a string in the format of "HH:mm:ss" (hours, minutes, seconds). That is all the data the date has in order to be created so other values, such as day, month, year, are filled in from the reference date (00:00:00 UTC on 1 January 2001). In your case, the breakfast date is "01/01/2000 00:00:01". However, the now date has other date components (day, month, year…) provided and so won't be between two dates 23 years ago.

With the code you have, you can just change the way you create the now variable like this:

var now = Date.parse(Date().dateString())

This extracts the provided components in the format string from the current date using your extensions on Date.

Oohh okay! Thanks for the clarification! I've been able to make it work.