updater
updater

Reputation: 997

Check time between 2pm and 8am

How to check if time is between 2pm and 8am ? This is not working:

value = Time.current.between?(Time.parse("2:00pm"), Time.parse("8:00am")) ? 9 : 4

If time is 1AM it should return 9, else 4(I want 9 if evening or night. If 'normal during day' - 4) Any idea?

Upvotes: 0

Views: 530

Answers (1)

jvnill
jvnill

Reputation: 29599

swap 2:00pm and 8:00am. first argument should be the lower value and 8:00am comes first before 2:00pm.

[3] pry(main)> Time.current
=> 2016-01-13 09:21:14 +0800
[4] pry(main)> Time.current.between?(Time.parse('9:00am'), Time.parse('2:00pm'))
=> true
[5] pry(main)> Time.parse('1:00am').between?(Time.parse('9:00am'), Time.parse('2:00pm'))
=> false

UPDATE

You can also use Time.current.hour.between?(9, 16)

Upvotes: 2

Related Questions