Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if date is within the past seven days

Tags:

date

ruby

I have a date in the following format: "%d-%m-%Y" (for example, today's date would be 07-09-2015), and I want to see if it is within the past seven days. Can anyone recommend a way?

like image 708
DMH Avatar asked Sep 07 '15 11:09

DMH


2 Answers

You can do it like this:

require "date"
Date.today - 7 <= Date.parse("07-09-2015", "%d-%m-%Y")
like image 174
sawa Avatar answered Sep 28 '22 06:09

sawa


sawa's answer is correct but In rails you can also check like this:

7.days.ago <= Date.parse("07-09-2015", "%d-%m-%Y")
#=> true 
like image 34
Gagan Gami Avatar answered Sep 28 '22 08:09

Gagan Gami