if (myDate > today) document.write('today is before myDate');
else document.write('today is on or after myDate');
But do you really want to use Javascript for that? That not only requires a Javascript client, it relies on the client's idea of what day it is.
function dateStringToTime(dateStr) {
year = dateStr.substring(6, 8);
month = dateStr.substring(0, 2);
day = dateStr.substring(3, 5);
return new Date(year, month, day).getTime();
}
This will turn your date string into a Unix timestamp. Then it can be numerically compared numerically with basic operators. i.e.: if(date1 > date2) { ... }
function dateStringToTime(dateStr) {
year = dateStr.substring(6, 8); // 08 will not parse correctly as a year
month = dateStr.substring(0, 2); // Date function uses months from 0-11
day = dateStr.substring(3, 5);
return new Date(year, month, day).getTime();
}
year = parseInt(dateStr.substring(6, 8), 10);The parseInt() call turns the string "02" into the integer 2, and the second argument to parseInt() means you want to force JavaScript to use base 10 (it will try to use octal conversion on "08" and "09" otherwise, because of the leading zero, and complain about invalidity).
month = parseInt(dateStr.substring(0, 2), 10) - 1;
day = parseInt(dateStr.substring(3, 5), 10);
var datePieces = dateStr.split("-");This just covers the string-to-date conversion part of the problem. You've gotten the right advice already as to the rest of it: create real Date objects and use those objects' built-in comparison function.
var month = parseInt(datePieces[0], 10) - 1,
day = parseInt(datePieces[1], 10),
year = parseInt(datePieces[2], 10);
leftBound = "10-14-08";
splitLeftBound = leftBound.split("-");
rightBound = "10-16-08";
splitRightBound = rightBound.split("-");
testDate = "10-15-08";
splitDate = testDate.split("-");
// Convert to number of seconds since 1970 - Note this breaks after 2099
leftTest = new Date(2000 + parseFloat(splitLeftBound[2]), splitLeftBound[0] - 1, splitLeftBound[1]).getTime();
rightTest = new Date(2000 + parseFloat(splitRightBound[2]), splitRightBound[0] - 1, splitRightBound[1]).getTime();
test = new Date(2000 + parseFloat(splitDate[2]), splitDate[0] - 1, splitDate[1]).getTime();
//You now have 3 comparable integers
if (test <> alert("Outside of left bound");
} else if (test > rightTest) {
alert("Outside of right bound");
} else {
alert("Number is good!");
}>
posted by bitdamaged at 2:27 PM on October 29, 2008