1

I have a site that uses ruby (rhtml) files which I am not used to editing (more used to php). I only have access to these files to edit (so not the back-end).

In the database I have a date that I can display using:

<%= zp.date %>

which will display e.g. 14/10/2010

I can display today's date using:

<%= Date.today.strftime("%Y%m%d") %>

which will display 20160122.

What I'm trying to work out is how I can compare the dates and echo the difference and in turn display something different.

e.g.

compare <%= zp.date %> and <%= Date.today.strftime("%Y%m%d") %>. 
If days difference is less than 20 echo XXX 
else 
If days difference between 20 - 40 echo XXX 
else
If days difference between 41 - 60 echo XXX 

Any help would be appreciated (links etc), code examples would be greatly appreciated.

Again to clarify I can only edit the rhtml.

Phil
  • 27
  • 8

1 Answers1

1

Dates are directly comparable with if-statements, and computing the difference will show you the number of days between them. Assuming zp.date is in the past:

<% date_difference = Date.today - zp.date %>
<% if date_difference < 20 %>
    ...
<% elsif date_difference <= 40 %>
    ...
<% elsif date_difference <= 60 %>
    ...
<% else %>
    Difference is greater than 60
<% end %>

Note the <% %> tags, which are used for if-logic and internal Ruby code - basically any expression that shouldn't be output (thanks @QPaysTaxes), instead of <%= %> which is the output tag.

If you don't know whether zp.date is in the past or future, you can use the abs method on the result:

<% date_difference = (Date.today - zp.date).abs %>

And if zp.date isn't actually a Date object, then parse it:

<% date_difference = (Date.today - Date.parse(zp.date)).abs %>

Older Ruby versions will need strptime when the date string is ambiguous:

<% date_difference = (Date.today - Date.strptime(zp.date, '%d/%m/%Y')).abs %>
sjagr
  • 15,983
  • 5
  • 40
  • 67
  • Hi, thank you for your help, I have tried the above but sadly I get a 500 error ( `-': expected numeric or date). Could this be due to the formats being different? e.g. 14/10/2010 compared to Date.today being 2016-01-22. Any way to change 14/10/2010 to 2010-10-14? – Phil Jan 22 '16 at 17:13
  • As a side note, it'd be more correct (and too pedantic to be worth "correcting") to say that `<%` is used for any expression whose result shouldn't be output. – Nic Jan 22 '16 at 17:13
  • @Phil If both are stored as `Date` objects, it won't matter. Make sure you make `Date` objects out of them, though -- this doesn't work on Strings. – Nic Jan 22 '16 at 17:14
  • How do I "Make sure you make Date objects out of them", sorry completely new to ruby. – Phil Jan 22 '16 at 17:17
  • @Phil Follow my latest edit. You probably need to parse it with `Date.parse`. A slash format like 14/10/2010 is pretty ambiguous though (i.e. 01/02/2010), see if you can get a better value like `2010-10-14`. – sjagr Jan 22 '16 at 17:17
  • Sadly still having errors. Just so I can check how would I just parse the <%= zp.date %> on it own so is displayed as 2010-10-14? Hoping I can then test and work out whats causing the issue. Again thanks for your help here. – Phil Jan 22 '16 at 17:26
  • @Phil `<%= Date.parse(zp.date).strftime('%Y-%m-%d') %>` – sjagr Jan 22 '16 at 17:56
  • Again thanks for your help. Sadly <%= Date.parse(zp.date).strftime('%Y-%m-%d') %> 500 errors out with : ruby/1.8/date.rb:956:in `new_by_frags': invalid date (ArgumentError): and lib/ruby/1.8/date.rb:1000:in `parse': – Phil Jan 22 '16 at 18:10
  • @Phil Time to inspect it. `<%= zp.date.inspect %>` – sjagr Jan 22 '16 at 18:10
  • <%= zp.date.inspect %> displays "14/10/2010" – Phil Jan 22 '16 at 18:15
  • @Phil See my edit. Make sure you tell someone that their Rails and Ruby versions seem to be very out of date. – sjagr Jan 22 '16 at 18:22
  • You sir... are a god! I bow to your ruby skills! :) Thanks again for all your help. – Phil Jan 22 '16 at 18:26
  • @Phil Glad to help! If my solution solved your problem, make sure to mark my answer as accepted. It's the grey "checkmark" below the up and down arrows next to my answer. Cheers! – sjagr Jan 22 '16 at 18:31