Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get yesterday's date

This may be an obvious question but im new to ruby and I've spent ages trying to get yesterdays date for a cucumber test that checks a url that includes yesterday's date. for example: http://www.blah.co.uk/blah/blah/schedule/2011-11-28

I've created the following within my helper methods:

def Helper.get_date
  Time.now.strftime('%Y-%m-%d') / 1.day
end

But it doesn’t like the /1.day or – 1.day or – 86400 (seconds).

like image 836
IRH301010 Avatar asked Jun 23 '26 21:06

IRH301010


2 Answers

You can use some of the methods in ActiveSupport (which will be included if you're in a Rails app) to do the following:

DateTime.yesterday.strftime('%Y-%m-%d')

EDIT: As it's a standard date format, you could also reference the standard directly:

DateTime.yesterday.iso8601
like image 71
Jon M Avatar answered Jun 25 '26 19:06

Jon M


(Date.today-1).strftime('%Y-%m-%d')

Would be a non rails-specific way.

like image 22
steenslag Avatar answered Jun 25 '26 18:06

steenslag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!