Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...