jQuery 如何用jQuery比较两个日期值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3004178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two date values with jQuery
提问by Mercer
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>
回答by samba
var startDt=document.getElementById("startDateId").value;
var endDt=document.getElementById("endDateId").value;
if( (new Date(startDt).getTime() > new Date(endDt).getTime()))
{
----------------------------------
}
回答by gnarf
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)
将日期字符串转换为 JavaScriptDate
对象,然后您可以使用标准<
和>
回答by Vikash Singh
var startDate = $('#start_date').val().replace(/-/g,'/');
var endDate = $('#end_date').val().replace(/-/g,'/');
if(startDate > endDate){
// do your stuff here...
}
回答by svg
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://stackoverflow.com/questions/2974496/jquery-javascript-convert-date-string-to-date
https://stackoverflow.com/questions/2974496/jquery-javascript-convert-date-string-to-date
cheers..!!
干杯..!!
回答by Darin Dimitrov
回答by timmz
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/.(在页面末尾)