View Full Version : Checking for a future date problem

09-13-2010, 04:45 PM
I have a problem checking that a date is in the past or future.

Everything seems fine but the alert always comes back saying the date is in the past.

function OpenTimesheet(TSdate)
var SelectedDate = new Date(TSdate)

if (SelectedDate > Date())
alert (SelectedDate+' is a AFTER '+Date())
alert (SelectedDate+' is a BEFORE '+Date())

TSdate is in yyyy/mm/dd format and looks fine, in both alerts the dates are written correctly, it just always says they are a 'BEFORE' date regardless.

Any help with this one would be greatly apprieciated.

Philip M
09-13-2010, 05:39 PM
Try this:-

<script type = "text/javascript">

function checkFutureDate(end_year, end_month, end_day) {
end_month = end_month -1; // months in Javascript are 0-11
var now = new Date().getTime();
var d = new Date();
d.setFullYear(end_year, end_month, end_day); // YYYY,MM(0-11),DD
var selectedDate = d.getTime(); // today or after
if (selectedDate <= now) { // valid after today's date
//if (selectedDate < now) { // valid on today's date or after
alert ("Date must be (on or) after today's date!");
return false;
alert ("Future date is valid");
return true;

checkFutureDate(2010,09,15); // 12th September 2010


"It is inaccurate to say that I hate everything. I am strongly in favor of common sense, common honesty, and common decency. This makes me forever ineligible for public office". H. L. Mencken - US editor (1880 - 1956)

09-13-2010, 06:01 PM
worked perfectly thanks, could you shed any light on what was wrong to start with?

Obviosuly splitting the date into yyyy,mm,dd has something to do with it and SetFullYear also must be doing something essential.

Philip M
09-13-2010, 07:02 PM
TSdate must be broken down into year,month,day.

Remember that in Javascript Months are 0-11.

function OpenTimesheet(yy,mm,dd){

var SelectedDate = new Date(yy,mm-1,dd);
var today = new Date();
if (SelectedDate > today) {
alert (SelectedDate+' is a AFTER '+Date());
else {
alert (SelectedDate+' is a BEFORE '+Date());

OpenTimesheet(2010,09,12); // 12th September 2010