Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two date values with jQuery [duplicate]

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> 
like image 998
Mercer Avatar asked Jun 09 '10 08:06

Mercer


People also ask

How can I compare two datetime strings?

If both the date/time strings are in ISO 8601 format (YYYY-MM-DD hh:mm:ss) you can compare them with a simple string compare, like this: a = '2019-02-12 15:01:45.145' b = '2019-02-12 15:02:02.022' if a < b: print('Time a comes before b.


2 Answers

var startDt=document.getElementById("startDateId").value; var endDt=document.getElementById("endDateId").value;  if( (new Date(startDt).getTime() > new Date(endDt).getTime())) {     ---------------------------------- } 
like image 194
samba Avatar answered Nov 08 '22 08:11

samba


If you are also using jQuery ui, in particular datepicker, you can use $.datepicker.parseDate(format, string) to turn your date strings into a JavaScript Date object, which you can then compare using the standard < and >

like image 41
gnarf Avatar answered Nov 08 '22 08:11

gnarf