How to compare two date values with jQuery

Mercer picture Mercer · Jun 9, 2010 · Viewed 204.8k times · Source

I have two String fields which represent Dates in my page and I would like to compare these two fields to know if my first date < second date. How can I do this?

<tr>
    <td align="right">First Date: </td>
    <td align="left"> <html:text name="addPublicationForm" styleId="firstDate" property="firstDate" maxlength="10"/></td>
</tr>
<tr>
    <td align="right">Second Date: </td>
    <td align="left"> <html:text name="addPublicationForm" styleId="secondDate" property="secondDate" maxlength="10"/></td>
</tr>

Answer

samba picture samba · Nov 22, 2011
var startDt=document.getElementById("startDateId").value;
var endDt=document.getElementById("endDateId").value;

if( (new Date(startDt).getTime() > new Date(endDt).getTime()))
{
    ----------------------------------
}