javascript 如何比较javascript中的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16914216/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:26:36  来源:igfitidea点击:

How to compare dates in javascript?

javascriptdate

提问by binil

i want to compare two dates. i used this code

我想比较两个日期。我用了这个代码

function addRow(dateval,bidchekval) {                       
    var val1 = document.getElementById(dateval);            

   var valcheck=document.getElementById(bidchekval).value;
   var val123=document.getElementById(dateval).value;

   if(val123 > valcheck ){
       alert("success");
   }
}

This is the code that i used but when change month then it will break.those two dates get using date picker. date format example:06-12-2013 15:12:15

这是我使用的代码,但是当更改月份时它会​​中断。这两个日期使用日期选择器。日期格式示例:06-12-2013 15:12:15

回答by Mukesh Soni

if((new Date(val123).getTime()) > (new Date(valcheck).getTime()) ){
       alert("success");
   }

回答by vikingmaster

You cannot compare strings, but you can compare Dateobjects.

您不能比较字符串,但可以比较Date对象。

var valcheck=document.getElementById(bidchekval).value;
var val123=document.getElementById(dateval).value;

var check = new Date(valcheck) ;
var check123 = new Date(val123) ;

if (check > check123){
  alwer("Success") ;
}