Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

compare two date using javascript

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

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 am always getting the else part, is this because i split the date from 28-04-2014 00:00 to 28-04-2014 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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]);

than use the if condition

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...