Compare two dates with javascript

I have two dates in which one format is dd-mm-yyyy hh:mm and the other is in the format dd-mm-yyyy (D1) fristly I split the date of the format dd-mm-yyyy hh:mm to get only the format dd-mm-yyyy (D2) then I compare the date of D2 and D1 as

 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"; } 

I always get the else part, is it because I divided the date from 28-04-2014 00:00 to 28-04-2014 ?

+5
javascript date
source share
8 answers
 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]); 

what to use if condition

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

It really needs

 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'); } 

WORKING DEMO

+5
source share

This is a string comparison that you are doing, not a date comparison

In terms of strings ... 2 is greater than 0 ... That's why you always make the error "something is wrong"

EDIT: This is an explanation of what went wrong // Creates a date variable 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"; } 

EDIT: This is a possible solution.,

Do it instead

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

EDIT: If you get it from the input field do this

 // 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"; } 
+2
source share

To compare the date in javascript, I have good or say very good javascript called

Date.js

GoogleProject - date.js

By including this js, you can simply compare and make each method the same as in C # or the side of the code. Also the above link gives a great example.

0
source share

Try:

 var msg = new Date("03-05-2014").getTime() >= new Date("28-04-2014 00:00").getTime() ? 'Correct' : 'Wrong'; alert(msg); 
0
source share
 var now = new Date(); var end = new Date(//some other date hear); if(now>end) { console.log('now=',now); } else { console.log('end=',end); } 
0
source share

You can compare the date as the easiest and most understandable way.

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

so first write a general method to analyze the date method.

  <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> 

here you can call the method above

 <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> 

Just use the parseDate method and compare any type of date.

0
source share

Convert them to milliseconds, then compare

-one
source share

All Articles