String date comparison

I am trying to compare dates that I have to the current day’s date so i
can know if something is over due.

this is my code:

<% if assignment[:due_date] < Date.today %>
<%= flash[:notice]=“OVERDUE” %>

the error I keep getting is comparison of String with Date failed.

Any ideas??? thanks in advance

Any ideas??? thanks in advance

assignment[:due_date] is a String. To compare dates try this:

if Time.parse(assignment[:due_date]) < Time.now

There may well be a Date.parse method, but I didn’t bother to look :slight_smile:

Jared Delux247 wrote:

I am trying to compare dates that I have to the current day’s date so i
can know if something is over due.

this is my code:

<% if assignment[:due_date] < Date.today %>
<%= flash[:notice]=“OVERDUE” %>

the error I keep getting is comparison of String with Date failed.

Any ideas??? thanks in advance

May try replacing “Date.today” with “Date.today.to_s”.
You’ll probably want to make sure the formatting is the same.

Date.today.class
=> Date

Date.today.to_s.class
=> String

Can you give us a preview of what assignment[:due_date]?

Philip H. wrote:

Any ideas??? thanks in advance

assignment[:due_date] is a String. To compare dates try this:

if Time.parse(assignment[:due_date]) < Time.now

There may well be a Date.parse method, but I didn’t bother to look :slight_smile:

Thank you Philip, worked like a charm!

On 15 Aug 2008, at 22:00, Jared Delux247 wrote:

the error I keep getting is comparison of String with Date failed.

what’s assignment? if it’s something from params then it will just be
a string and you’ll have to convert it to a Date first (Date.parse
etc…)

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs