How to compare two date values with jQuery [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-26 11:01:05

问题


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>

回答1:


var startDt=document.getElementById("startDateId").value;
var endDt=document.getElementById("endDateId").value;

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



回答2:


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 >




回答3:


var startDate = $('#start_date').val().replace(/-/g,'/');
var endDate = $('#end_date').val().replace(/-/g,'/');

if(startDate > endDate){
   // do your stuff here...
}



回答4:


just use the jQuery datepicker UI library and convert both your strings into date format, then you can easily compare. following link might be useful

https://stackoverflow.com/questions/2974496/jquery-javascript-convert-date-string-to-date

cheers..!!




回答5:


Once you are able to parse those strings into a Date object comparing them is easy (Using the < operator). Parsing the dates will depend on the format. You may take a look at Datejs which might simplify this task.




回答6:


check the solution provided here it may help, i use it in my projects. http://trentrichardson.com/examples/timepicker/ .(in the end of the page)



来源:https://stackoverflow.com/questions/3004178/how-to-compare-two-date-values-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!