74

I'm trying to make my own calendar because i cant seem to get any help with event calendar so what im trying to do is Display the day of the week ie.. January 1 2011 is a Saturday is there an easy command to get that day or do i need something else

6 Answers 6

163

You can also get the string, like "Wednesday", using time.strftime("%A")

0
135

Can't you use time.wday, 0 = sunday and so on

1
  • 2
    that works even better than Date::DAYNAMES[month] thanks for the help
    – Rod Nelson
    Commented Jan 30, 2011 at 18:21
10

Actively using in Rails 4 - should in most other versions as well...

Both of these options will give you the day string (eg. "Saturday") for the date specified:

Specific date:

Date.new(2011, 1, 1).strftime('%A') # returns "Saturday"

Today:

Date.today.strftime('%A')
0
4

If you're attempting to write your own calendar from scratch and want to write a function to do day lookup, you might want to check out Conway's Doomsday Algorithm, which is an interesting method for determining the day of the week on any given date. Otherwise the standard time class has a wday method which returns a number from 0-6 (0 is Sunday, 1 is Monday, etc).

1
  • 1
    i found Date.today.beginning_of_month.wday works exactly the same way for what i was looking for but i will take a look at your link just to see if it might make my life easier.
    – Rod Nelson
    Commented Jan 30, 2011 at 18:48
4

We can use Time.now.utc.wday to get day of week without considering zone.

1

No need to use the Time class. date.cwday will return 1 for Monday, 2 for Tuesday etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.