Comparing times only, without dates?

后端 未结 8 1395
不知归路
不知归路 2021-01-31 16:30

I need to write a method that will check if Time.now is in between the open hours and the close hours of a shop.

The open and close hours are saved as a Tim

8条回答
  •  你的背包
    2021-01-31 17:06

    You can strip the Time into its hours, minutes and seconds.

    As described in Time Class:

    t = Time.now
    hour = t.hour
    minute = t.min
    seconds = t.sec
    

    Since you need to just compare whether it's within 2 hours you can check it as below.

    if hour > openingHour and hour < closingHour
    

提交回复
热议问题