处理午夜时间NSDate()

时间:2015-12-23 07:53:09

标签: swift nsdate nscalendar

我正在执行打开/关闭状态功能,检查并比较当前时间与自助餐厅的营业时间。但它一直营业到午夜。午夜表示为0如何将其与0进行比较? 我目前的代码

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
    }

0 个答案:

没有答案