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

前端 未结 10 620
抹茶落季
抹茶落季 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:33

    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"
    

提交回复
热议问题