p.sivakumar
p.sivakumar

Reputation: 35

how to compare two different date in jquery

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) {}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>

how to compare two date and check different condition i got

Upvotes: 1

Views: 7979

Answers (3)

You will need to declare both var as Date, now you are just comparing string to string, here i have made an example: 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"); 
    }
});

});

Upvotes: 2

a.barbieri
a.barbieri

Reputation: 2596

Maybe this is what you are looking for.

I'm using 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>

Upvotes: 1

Furquan Khan
Furquan Khan

Reputation: 1594

<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)
 {
 }

Upvotes: 0

Related Questions