Search code examples
ruby-on-railsapiruby-on-rails-5

how to get date by day number or day name in rails?


is there a built-in method that give me a date of the day by giving it the day name or number ex: input: Saturday => output: 28-3-2020


Solution

  • Try this

    Date.parse('Saturday').strftime('%d-%-m-%Y')
    #=> "28-3-2020"
    

    From comments, may try this approach to get a week's data

    date_by_day = Date.parse('Saturday')
    #=> Sat, 28 Mar 2020
    
    start_date = date_by_day - 7
    #=> Sat, 21 Mar 2020
    
    end_date = start_day + 6
    #=> Fri, 27 Mar 2020
    

    Hope that helps!

    It will give you the date of next Saturday.