如何比较两个日期值与jQuery [重复]

时间:2022-09-14 12:06:42

This question already has an answer here:

这个问题在这里已有答案:

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?

我有两个字符串字段代表我的页面中的日期,我想比较这两个字段,以了解我的第一个日期 <第二个日期。我怎样才能做到这一点?< p>

<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>

6 个解决方案

#1


51  

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

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

#2


24  

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 >

如果您还使用jQuery ui,特别是datepicker,则可以使用$ .datepicker.parseDate(format,string)将日期字符串转换为JavaScript Date对象,然后可以使用标准 <和> 进行比较

#3


7  

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

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

#4


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

只需使用jQuery datepicker UI库并将您的字符串转换为日期格式,然后就可以轻松比较。以下链接可能有用

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

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

cheers..!!

干杯..!!

#5


2  

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.

一旦您能够将这些字符串解析为Date对象,就可以轻松比较它们(使用 <运算符)。解析日期将取决于格式。您可以查看可能简化此任务的datejs。< p>

#6


2  

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)

检查这里提供的解决方案可能有所帮助,我在我的项目中使用它。 http://trentrichardson.com/examples/timepicker/。(在页面末尾)

#1


51  

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

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

#2


24  

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 >

如果您还使用jQuery ui,特别是datepicker,则可以使用$ .datepicker.parseDate(format,string)将日期字符串转换为JavaScript Date对象,然后可以使用标准 <和> 进行比较

#3


7  

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

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

#4


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

只需使用jQuery datepicker UI库并将您的字符串转换为日期格式,然后就可以轻松比较。以下链接可能有用

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

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

cheers..!!

干杯..!!

#5


2  

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.

一旦您能够将这些字符串解析为Date对象,就可以轻松比较它们(使用 <运算符)。解析日期将取决于格式。您可以查看可能简化此任务的datejs。< p>

#6


2  

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)

检查这里提供的解决方案可能有所帮助,我在我的项目中使用它。 http://trentrichardson.com/examples/timepicker/。(在页面末尾)