View Full Version : How to check past date?
buckbeak
10-07-2002, 08:40 AM
I want to compare the date entered and today's date, if the date entered is past date, pop up alert("Cannot be a past date")
the variable with ## are cf variable
function ShowDate(DayOfMonth)
{
<CFOUTPUT>
var DateToShow = "#month#/" + DayOfMonth + "/#year#";
var dateYear = #year#;
alert(dateYear);
var dateMonth = #month#;
alert(dateMonth);
var dateDay = DayOfMonth;
var enteredDate = new Date(dateYear, dateMonth, dateDay);
alert(enteredDate.getDate());
var today = new Date();
//alert(today);
if (today.getDate() > enteredDate.getDate())
{
alert("Cannot be a past date !");
}
else {
window.opener.document.forms[0].DateField.value = DateToShow;
window.close();}
</CFOUTPUT>
}
What did I do wrong?Please help!
glenngv
10-07-2002, 11:49 AM
you're only comparing the date (1-31) not the whole date.
change:
if (today.getDate() > enteredDate.getDate())
to:
if (today > enteredDate)
buckbeak
10-08-2002, 02:12 AM
i can't get the enteredDate correctly it shows Nov ??How come?
glenngv
10-08-2002, 03:02 AM
can you post the generated javascript code of the cold fusion?
buckbeak
10-08-2002, 03:23 AM
Here's my code:-
<SCRIPT LANGUAGE = "JavaScript">
<!--
// function to populate the date on the form and to close this window. --->
function ShowDate(DayOfMonth)
{
<CFOUTPUT>
now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());
DateToShow = new Date(#year#, #month#, DayOfMonth);
var enteredDate = new Date(DateToShow.getYear(), DateToShow.getMonth(), DateToShow.getDate());
alert(enteredDate);
if (today > enteredDate)
{
alert("Cannot be a past date !");
}
else {
var DateToShow = "#month#/" + DayOfMonth + "/#year#";
window.opener.document.forms[0].DateField.value = DateToShow;
window.close();}
</CFOUTPUT>
}
//-->
</SCRIPT>
as for the #month#, i tested it when i put alert(#month#) the output is 10 ( this is correct) but the output of enteredDate is Nov(11)?? Why is that?
Originally posted by buckbeak
as for the #month#, i tested it when i put alert(#month#) the output is 10 ( this is correct) but the output of enteredDate is Nov(11)?? Why is that? Hi buckbeak,
There are several ways for the Date constructor to create a Date object.
In the one you are using (arguments are coma separated numbers),
it expects the month argument (2nd arg) to be the same as the
Date object stores it, i.e. zero indexed.
So, when you enter 10 expecting Oct., the zero indexed month is
actually 11 (Nov).
It can get pretty confusing because for the argument of the date
of month (3rd arg) it expects a NON zero indexed argument.
To realize try this:
alert(new Date(2002,1,1))
See, for the month it gave you one ahead of the human 1st month
but it gave you the correct date.
It's the reverse when you try to get what it stores for a certain date:
alert('Todays month is ' + new Date().getMonth())
alert('Todays date of month is ' + new Date().getDate())
Here it gave you one month behind the human one but the date
is correct.
Or, as you already realized in your case.
Now, to fix your function.
Because it can be confusing, and because users will always
enter the date in "human" input, the Date constructor is designed
to create dates also from "human" arguments.
From the few ways it can be done, I recommend the following:
new Date("month/date/year") // note argument is a string
simply because you have to construct this argument in any case.
If you use it this way, your function can look like so:function ShowDate(DayOfMonth)
{
<CFOUTPUT>
DateToShow = "#month#/" + DayOfMonth + "/#year#";
if (new Date() > new Date(DateToShow))
alert("Cannot be a past date !");
else {
window.opener.document.forms[0].DateField.value = DateToShow;
window.close();}
</CFOUTPUT>
} ( •) (• )
>>V
Javascript counts from zero so -1
buckbeak
10-09-2002, 02:30 AM
Thanks Owl! It's works perfectly! :D
buckbeak
10-09-2002, 02:40 AM
What if it is today?
i mean new Date() == new Date(DateToShow())??
when i select today the alert will pop up..
i.e 10/9/02 < 10/9/02 ----> how does js interpret this??
glenngv
10-09-2002, 02:59 AM
false.
var today = new Date(); // 10/9/2002
var enteredDate = new Date("10/9/2002");
alert(today<enteredDate); //false
buckbeak,
In the function I suggested, todays date will be considered as past.
This is because new Date() have the exact time of the day
(which is likely to be something), while new Date(DateToShow)
is default to 0 time (beginning of the day).
If you want user todays date not to be considered past, we have
to create our todays date with 0 time:function ShowDate(DayOfMonth)
{
<CFOUTPUT>
DateToShow = "#month#/" + DayOfMonth + "/#year#";
d=new Date()
d=new Date(d.getYear(), d.getMonth(), d.getDate())
if (d > new Date(DateToShow))
alert("Cannot be a past date !");
else {
window.opener.document.forms[0].DateField.value = DateToShow;
window.close();}
</CFOUTPUT>
}( •) (• )
>>V
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.