Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Determine the day of the week

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

like image 713
fardin Avatar asked Mar 11 '16 19:03

fardin


3 Answers

In Ruby 2.1.1, the Date class has a friday? method

Returns true if the date is a friday

First, require the date library

require 'date'

Then create a new date instance with the current date. Here's an example

current_time = Time.now
year = current_time.year
month = current_time.month
day = current_time.day

date = Date.new(year, month, day)
date.friday?
=> true

Depending on your coding preferences, you could DRY this up even more

date = Date.new(Time.now.year, Time.now.month, Time.now.day)
=> #<Date: 2016-03-11 ((2457459j,0s,0n),+0s,2299161j)>
date.friday?

If you'd like to solve this without a Boolean method, (the ones that usually end in a question mark and return either true or false), such as when you're comparing to a database column, you can use the Date#wday method. Keep in mind, this returns a number in the range (0..6) with 0 representing Sunday. This means that you want to pass in 5 to check for Friday.

if date.wday == 5
  // Do something
end

Also, if you are working with business hours, it might be easiest to use the business_time gem

You can also include the holidays gem with business_time.

First, install the gems

gem install business_time
gem install holidays

Then require the gem

require 'business_time'
require 'holidays'

Find out if today is a workday

Date.today.workday?

and is a holiday

You can now use something like this to determine if today is a holiday

Holidays.on(date, :us).empty?

and is between office working hours

The definition of office hours varies from person to person. There's no set-in-stone answer. However, with the business_time gem, you can set configurations

BusinessTime::Config.beginning_of_workday = "8:30 am"
BusinessTime::Config.end_of_workday = "5:30 pm"

Sources

Ruby

http://ruby-doc.org/stdlib-2.1.1/libdoc/date/rdoc/Date.html#method-i-wday

http://ruby-doc.org/stdlib-2.1.1/libdoc/date/rdoc/Date.html#method-i-friday-3F

Gems

https://github.com/bokmann/business_time

https://github.com/holidays/holidays

like image 117
Richard Hamilton Avatar answered Nov 03 '22 06:11

Richard Hamilton


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

like image 23
Phil Maddaloni Avatar answered Nov 03 '22 06:11

Phil Maddaloni


Check this-

require 'date'
today = DateTime.current.to_date

if today.Friday?
  puts "Today is a Friday!"
end
like image 25
Mohammad Shahadat Hossain Avatar answered Nov 03 '22 06:11

Mohammad Shahadat Hossain