如何在jquery

时间:2017-06-01 08:20:05

标签: jquery html5



var date = $("#date").text();
// this date  is variable 01/06/2017


// this  date is also  will be variable
var startdate = 01 / 06 / 2017
if (date == startdate) {}
if (date < startdate) {}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
&#13;
&#13;
&#13;

如何比较两个日期并检查我得到的不同条件

3 个答案:

答案 0 :(得分:1)

您需要将两个var声明为Date,现在您只是将字符串与字符串进行比较,这里我举了一个例子:https://cg6.tutorialspoint.com/share/661/IytBM5oT

$(document).ready(function(){

$("button").click(function(){

    var date = new Date($("#date").val());

    var startdate = new Date("11/01/2014");

    if(date < startdate)
    {
     $("#datevalueresault").text("startdate is SMALLER than input");
    }
    if(date > startdate)
    {
       $("#datevalueresault").text("startdate is BIGGER than input"); 
    }
    if(date.getTime() === startdate.getTime())
    {
       $("#datevalueresault").text("both date are the SAME"); 
    }
});

});

答案 1 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
 var date =new Date($("#date").text());
 // this date  is variable 01/06/2017


 // this  date is also  will be variable
 var startdate= new Date(2017,5,1)//5 is for sixth month
 if (date==startdate)
 {
 }
 if (date<startdate)
 {
 }

答案 2 :(得分:0)

也许这就是你要找的东西。

我正在使用Date.parse()

var date = Date.parse( $("#date").text() )
var startdate = Date.parse( $("#startdate").text() )

if (date == startdate) {
  $('#comparison').text('same day')
}
if (date < startdate) {
  $('#comparison').text('previous day')
}
if (date > startdate) {
  $('#comparison').text('next day')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>


<p>What is <span id="date">31 may 2017</span> compared to <span id="startdate">1 jun 2017 </span></p>
<p id="comparison"></p>