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

前端 未结 10 623
抹茶落季
抹茶落季 2020-12-01 05:05

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

10条回答
  •  没有蜡笔的小新
    2020-12-01 05:45

    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
    

提交回复
热议问题