Comparing two Dates in javascript

Posted by Samy33 under JavaScript on 11/1/2010 | Points: 10 | Views : 73030 | Status : [Member] | Replies : 2
I am in new in javascript. i am having two dates date1 and date2 .when date1 is selecting from the user date2 is current date.when date1 is greater than date2 means its shows error like Date1 should be less than Current date.it shows Correctly.But i am selecting less than current date it shows same error.only it allows current date,I need help from you for comparing dates


this is my Script

var today=new Date();
var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
var DateFormat = today.getDate();
if (DateFormat < 10)
{
var now ="0"+ today.getDate() + "/" + monthtext[today.getMonth()] + "/" + today.getYear();

}
else
{
var now = today.getDate() + "/" + monthtext[today.getMonth()] + "/" + today.getYear();
}
//alert(now)
if(dateChoosed > now)
{
alert('Date Should be less than Current Date')
}
else
{
valueFld[0].value = dateChoosed;
}
}
in above datechoosed is came from the front end aspx page.




Responses

Posted by: Lalji_mer on: 11/1/2010 [Member] Bronze | Points: 25

Up
0
Down
here sample code you will get some idea

var Date1 = new Date("12/31/2004");
var Date2 = new Date("1/5/2005");

if (Date2 > Date1)
{
alert('found');
}


check also this link
http://www.experts-exchange.com/Web_Development/Miscellaneous/Q_20636385.html

LP MER

Samy33, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvrsri on: 11/1/2010 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down
http://www.dotnetfunda.com/codes/code235-function-to-compare-two-dates-.aspx
This link will help you

Cheers!!!

Thanks,
Radha Srikanth

Samy33, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response