Thread7 Thread7 - 7 months ago 25
Ruby Question

Converting a date to string representation

I have a date like this:

Date.today - 7


I tried to convert it into a string:

@last_week = strftime((Date.today - 7), '%Y-%m-%d')


But I get the error
"undefined method `strftime'"
. What am I doing wrong?

Answer

You can do it like this:

@last_week = (Date.today - 7).strftime('%Y-%m-%d')