Rails - Determine the day of the week

Viewed 6367

So DateTime.current returns Fri, 11 Mar 2016 19:34:10 +0000.

How can I determine the day of the week. For example, if DateTime.current is Friday(just the day of the week, regardless of the date)?

DateTime.current == DateTime.parse("Friday") Won't work because DateTime.parse("Friday") returns Fri, 11 Mar 2016 00:00:00 +0000 which is not the same.

How can I check whether only the date or only the time equals to a specific value?

Thanks in advance!

Basically I want to see if DateTime.current is not a weekend nor a public holiday and is between office working hours

3 Answers

You can use something like the following:

day = DateTime.now.strftime("%A")

This will return the day of the week with the full name, capitalized. You can also get the abbreviation like this:

day = DateTime.now.strftime("%a")

You can also use %u or %w to get the day of the week as an integer. Friday would be 5.

DateTime.now.strftime("%u") == 5 # True if Friday

You can check here for more capabilities:

APIdock documentation

Related