使用JavaScript比较两个日期

5

我有两个日期,一个是 dd-mm-yyyy hh:mm 格式,另一个是 dd-mm-yyyy (D1) 格式。 首先,我将 dd-mm-yyyy hh:mm 格式的日期拆分为只包含dd-mm-yyyy (D2)格式的日期。 然后,我比较日期 D2 和 D1,如下:

var D1 = new Date(); 
var D2 = new Date(); 
// D1 = 03-05-2014  this date take as an example
// D2 = 28-04-2014 00:00  this date take as an example
// D1 and D2 are taken by input fields.
    split the D2 date

dat = D2.split(' ');
D2 = dat[0];
//finally D2 is 28-04-2014
if(D2<=D1)
{
  echo "ok";
}
else{
  echo "something is wrong";
}

我总是得到else部分的结果,这是因为我将日期从28-04-2014 00:00拆分成了28-04-2014吗?


你需要什么类型的结果作为输出? - underscore
if条件语句中的true部分 - Mr. Tomar
实际上它只比较日期,我不知道为什么,它不比较整个日期。 - Mr. Tomar
请查看https://dev59.com/-n_aa4cB1Zd3GeqP00J6#23402449。 - Etheryte
没有得到答案。。 - Mr. Tomar
如果D2是一个日期,你怎么能对它进行分割呢? - NitinSingh
7个回答

7
dateFirst = D1.split('-');
dateSecond = D2.split('-');
var value = new Date(dateFirst[2], dateFirst[1], dateFirst[0]); //Year, Month, Date
var current = new Date(dateSecond[2], dateSecond[1], dateSecond[0]);

使用if条件语句

if(D2<=D1)
{
console.log('ok');
}
else
{
console.log('something is wrong');
}

5
这是您实际需要的内容。
var D1 = "03-05-2014";
var D2 = "28-04-2014 00:00";

if ((new Date(D1).getTime()) >= (new Date(D2).getTime())) {
    alert('correct');
} else {
    alert('wrong');
}

演示


通过这种方式,我不需要拆分日期来从“28-04-2014 00:00”中获取“28-04-2014”。 - Mr. Tomar
将任何日期字符串解析为对象并比较时间。 - underscore
2
你不需要使用 _getTime_,因为 DatevalueOf 已经被定义好了。 - Paul S.

2

你正在进行字符串比较,而不是日期比较。

就字符串而言... 2 大于 0... 这就是为什么你总是会遇到“出了点问题”的语句。

编辑:这是出错的解释 // 创建一个日期变量 var D1 = new Date();

// Creates another date variable
var D2 = new Date();

// Converts this date variable into a string
D1 = 03-05-2014 

// This is too is converted into a string
D2 = 28-04-2014 00:00

    dat = D2.split(' ');
D2 = dat[0];

//finally D2 is 28-04-2014  <-- true, but it is a string
if(D2<=D1){    //   <-- At this point you are doing a string comparison

        echo "ok";

} else {

    echo "something is wrong";

}

编辑:这是一种可能的解决方案...

不要那样做,而是这样做。

var D1 = new Date();
var D2 = new Date();

if(D2.getTime() <= D1.getTime()){
    echo "ok";
} else {
    echo "something is wrong";
}

编辑:如果您是从输入字段获取它,请执行以下操作

// Make sure you dates are in the format YYYY-MM-DD.
    // In other words, 28-04-2014 00:00 becomes 2014-04-28 00:00
// Javascript Date only accepts the ISO format by default
var D1 = new Date( $('#input1#').val() );
var D2 = new Date( $('#input2#').val() );

    if(D2.getTime() <= D1.getTime()){
    echo "ok";
} else {
    echo "something is wrong";
}

这是一条注释 - underscore
我使用变量来存储日期类型。 - Mr. Tomar
Tarzan,你一开始确实将它设为日期……但后来你使用了字符串D1 = 03-05-2014。请查看我的答案以获取解释和解决方案。 - Abimbola Esuruoso
不,我是从输入字段中获取日期的,这里只是举例说明。 - Mr. Tomar

0

尝试:

var msg = new Date("03-05-2014").getTime() >= new Date("28-04-2014 00:00").getTime() ? 'Correct' : 'Wrong';
alert(msg);

0

你可以使用最简单易懂的方式进行日期比较,例如:

<input type="date" id="getdate1" />   //with time include
<input type="date" id="getdate2" /> // without time include

首先编写一个通用的解析日期方法。

    <script type="text/javascript">
                function parseDate(input) {   
                  var parts = input.split(' ');
              var datecomp=parts[0];  // will get 
              var timecomp=parts[1];  // will get time
                  var dparts=datecomp.split('-');
             if(timecomp!=undefined && timecomp!=null){ //if there is like
                             var tparts=timecomp.split(':');
     return new Date(dparts[2], dparts[1]-1, dparts[0], tparts[0], tparts[1]); 


 }else{  // for when no time is givin
    return new Date(dparts[2], dparts[1]-1, dparts[0]); 
                                              }
                                             return "";
                                              }
            </script>

通过这里,您可以调用上述方法。

<script type="text/javascript">

              $(document).ready(function(){
              //parseDate(pass in this method date);
                    Var Date1=parseDate($("#getdate1").val());
                        Var Date2=parseDate($("#getdate2").val());
               //use any oe < or > or = as per ur requirment 
               if(Date1 = Date2){
         return false;  //or your code {}
}
 });
    </script>

只需使用parseDate方法并比较任何类型的日期。


0
var now = new Date();
var end = new Date(//some other date hear);
if(now>end)
{
     console.log('now=',now);
}
else
{
    console.log('end=',end);
}

-1

将它们转换为毫秒,然后进行比较。


你会怎么做? - martin
这应该是一条注释。如果您愿意,可以通过添加更详细的信息和/或示例将其转换为完整的答案。 - mathielo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接