Search code examples
rubydatetimehour

How to get user hour of the week in Ruby


I want to get the current hour of the week in which day starts from Sunday.

Consider current time is

Time.now
 => 2014-10-29 12:09:23PM +0530 

The result should be : 84

Explanation:

Sunday - 24 hours
Monday - 24 hours
Tuesday - 24hours
Wednesday - 12 hours

Total: 84

How can get the user hour of the week. Is there any method available in Ruby ? Or how to do it without Ruby method.


Solution

  • You can get the day of the week and hour of the day using Time#wday and Time#hour.

    Time.now.wday
    #=> 3
    Time.now.hour
    #=> 14
    

    The rest is basic mathematics.