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

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

Asked on December 27, 2018 in Ruby.
Add Comment


  • 6 Answer(s)

    By using date function we can achieve this easily,

    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
    
    Answered on December 27, 2018.
    Add Comment

    Have a look at this code,

    time = Time.at(time) # Convert number of seconds into Time object.
    puts time.wday # => 0: Day of week: 0 is Sunday
    
    Answered on December 27, 2018.
    Add Comment

     

    Try to use this solution,

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

     

    Answered on December 27, 2018.
    Add Comment
    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
    Answered on May 20, 2019.
    Add Comment
    Date.today.strftime("%A")
    => "Wednesday"
    
    Date.today.strftime("%A").downcase  
    => "wednesday"
    Answered on May 20, 2019.
    Add Comment
    def weekday(date_string)
      Date.parse(date_string).strftime("%A")
    end
    Answered on May 20, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.