How can I calculate the day of the week of a date in ruby?

RubyDate

Ruby Problem Overview


How can I calculate the day of the week of a date in Ruby? For example, October 28 of 2010 is = Thursday

Ruby Solutions


Solution 1 - Ruby

I have used this because I hated to go to the Date docs to look up the strftime syntax, not finding it there and having to remember it is in the Time docs.

require 'date'
     
class Date
  def dayname
     DAYNAMES[self.wday]
  end
        
  def abbr_dayname
    ABBR_DAYNAMES[self.wday]
  end
end
        
today = Date.today
        
puts today.dayname
puts today.abbr_dayname

Solution 2 - Ruby

Take a look at the Date class reference. Once you have a date object, you can simply do dateObj.strftime('%A') for the full day, or dateObj.strftime('%a') for the abbreviated day. You can also use dateObj.wday for the integer value of the day of the week, and use it as you see fit.

Solution 3 - Ruby

time = Time.at(time)    # Convert number of seconds into Time object.
puts time.wday    # => 0: Day of week: 0 is Sunday

Solution 4 - Ruby

Date.today.strftime("%A")
=> "Wednesday"

Date.today.strftime("%A").downcase  
=> "wednesday"

Solution 5 - Ruby

Quick, dirty and localization-friendly:

days = {0 => "Sunday",
1 => "Monday", 
2 => "Tuesday",
3 => "Wednesday",
4 => "Thursday",
5 => "Friday",
6 => "Saturday"}

puts "It's #{days[Time.now.wday]}"

Solution 6 - Ruby

Works out of the box with ruby without requiring:

Time.now.strftime("%A").downcase #=> "thursday"

Solution 7 - Ruby

As @mway said, you can use date.strftime("%A") on any Date object to get the day of the week.

If you're lucky Date.parse might get you from String to day of the week in one go:

def weekday(date_string)
  Date.parse(date_string).strftime("%A")
end

This works for your test case:

weekday("October 28 of 2010") #=> "Thursday"

Solution 8 - Ruby

Say i have date = Time.now.to_date then date.strftime("%A") will print name for the day of the week and to have just the number for the day of the week write date.wday.

Solution 9 - Ruby

In your time object, use the property .wday to get the number that corresponds with the day of the week, e.g. If .wday returns 0, then your date is Sunday, 1 Monday, etc.

Solution 10 - Ruby

basically the same answer as Andreas

days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
today_is = days[Time.now.wday]

if today_is == 'tuesday'
  ## ...
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManganoView Question on Stackoverflow
Solution 1 - RubysteenslagView Answer on Stackoverflow
Solution 2 - RubymwayView Answer on Stackoverflow
Solution 3 - Rubybrian_dView Answer on Stackoverflow
Solution 4 - RubyAbhiView Answer on Stackoverflow
Solution 5 - RubyAndreas BergströmView Answer on Stackoverflow
Solution 6 - RubyNiels KristianView Answer on Stackoverflow
Solution 7 - RubyAdam TannerView Answer on Stackoverflow
Solution 8 - RubySaqib R.View Answer on Stackoverflow
Solution 9 - RubyGeorge JohnstonView Answer on Stackoverflow
Solution 10 - RubyalillandView Answer on Stackoverflow