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!

Date comparison question.

Status
Not open for further replies.

JohnnyBGoode

Programmer
May 1, 2002
76
CA
I have a date variable. First of all, it probably need to be formatted in some way before the comparison is done.

How can I compare a date parameter (temp_date) to see if it is earlier or later than the current date?
 
If you have the date you wanna compare in string format, you can make it into a Date object like so:
Code:
var str = '03/09/2004';
var dte = new Date();
dte.setTime(Date.parse(str));
Take the date that you wanna compare and pass it to this function. It will return true if it's greater, and false if not.
Code:
function compareDates(dte) {
   var today = new Date(); //today's date
   if (dte.getTime() > today.getTime()) {
      alert("Date is greater than today");
      return true;
   }
   else {
      alert("Date is not greater than today");
      return false;
   }
}

-kaht

banghead.gif
 
Actually, one more question. Is there a way to subtract a day from a date? For instance, if I have the variable "dte" (as above), how can I subtract one day from it and get the previous days date? "dte" is already a valid dateTime format.
 
I found something that works. It may not be the best solution, but the following will subtract one day from a date variable:

strDate.setUTCDate(strDate.getUTCDate()-1);

More of an FYI. Thanx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top