Rails undefined method `strftime' for "2013-03-06":String

23,626

Solution 1

It looks like your duedate is a string, when strftime is a method for Time/Date class. You can try this:

Date.parse(task.duedate).strftime("%B %e, %Y")

Solution 2

This solved the issue for me:

Date.created_at.try(:strftime, ("%B %e, %Y"))

Hope this helps!

Share:
23,626
Colin
Author by

Colin

Updated on July 09, 2022

Comments

  • Colin
    Colin almost 2 years

    I am getting the error

    undefined method `strftime' for "2013-03-06":String

    when trying to display a date normally (June Sunday 3, 2013 or something similar) from the string 2013-03-06 using strftime.

    The line that does this in my index.html.erb and looks like this

    <td><%= task.duedate.strftime("%B %e, %Y") %></td>

    I am just learning Rails so I am sure it is just a stupid beginners error, any help would be appreciated. Thank you

    • Hackerman
      Hackerman about 11 years
      Example: d = Date.today # 2008-08-12 d.strftime(‘%b %d, %Y’) # will print August 12, 2008
    • Marcelo De Polli
      Marcelo De Polli about 11 years
      Is there a special reason why your duedate field is not of the Date or DateTime types?
    • thewillcole
      thewillcole almost 11 years
      I just ran into this issue after adding and populating a date-column in a migration. The solution was simply restarting (I guess Rails didn't know the attribute was a date until then?).
  • Xander
    Xander over 5 years
    This worked for me - since sometimes values are a null 😁 Thank you