Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple question........Compare Dates 1

Status
Not open for further replies.

Magnus53

Programmer
Jul 25, 2002
73
CA
Hi guys,

Sorry of this has been asked before, I searched and looked in the Faq but couldn't find the answer.

I have 3 javascript date objects, dateToCheck, startDate, endDate

I need to check if dateToCheck falls between startDate and the endDate.

I have this but it doesn't seem to work:
if dateToCheck > startDate && dateToCheck < endDate {
then do something......
else
do somethign else

the if statement always fails, it doesn't give me any error that I can see, and when I view the dates it they are correct. how do I compare them?

Thanks in advance
 
Code:
Date.getTime()
is your friend in this case. It returns the date from January 1st, 1970 to the date object it is applied to, in milliseconds.

Example:
Code:
var start_date =new Date(2000, 0, 1);
var date_to_check =new Date(2001, 0, 1);
var end_date =new Date(2002, 0, 1);

if( start_date.getTime() < date_to_check.getTime() && end_date.getTime() > date_to_check.getTime() )  {

	alert( &quot;Falls between the dates.&quot; );
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top