0

I am doing Open /Close status function that checks and compares current time with open hours of the cafeteria. But it is open till midnight. Midnight represents as 0 how do I compare it to 0? My current code

func displayStatusForBrandywine () -> String
    {
        let currentDateTime = NSDate()
        // get the user's calendar
        let userCalendar = NSCalendar.currentCalendar()
        // choose which date and time components are needed
        let requestedComponents: NSCalendarUnit = [
            NSCalendarUnit.Year,
            NSCalendarUnit.Month,
            NSCalendarUnit.Day,
            NSCalendarUnit.Hour,
            NSCalendarUnit.Minute,
            NSCalendarUnit.Weekday
        ]
        let dateTimeComponents = userCalendar.components(requestedComponents, fromDate: currentDateTime)
        var status: String = ""
        // get the components

        if (dateTimeComponents.hour >= 8 && dateTimeComponets.hour <0) //??
        {
            status = "Open"
        }
        else
        {
            status = "Closed"
        }
        return status
    }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Yaroslav Dukal
  • 3,894
  • 29
  • 36
  • 1
    Why not swap the `if` with the `else` and use this condition instead `dateTimeComponents.hour < 8 && dateTimeComponets.hour >=0` – hannad Dec 23 '15 at 07:55
  • very clever idea! did not think that way. Thank you!!! – Yaroslav Dukal Dec 23 '15 at 07:56
  • However, to answer your question properly, check out this [stack overflow question](http://stackoverflow.com/q/26189656/5363378) – hannad Dec 23 '15 at 08:02

0 Answers0