...

View Full Version : Date not in the past



ellisd5
02-10-2004, 01:58 PM
Whats the easyiest way to check that a date is not in the past?

I want to check that my date is in this format "DD/MM/YYYY" and is not in the past.

Is there a easy way to do this with RegEx.

I was thinking of creating a new Date object and with the inputed value and then checking that new Date() is not create than it. However that will check from the exact second wont it, So how can I tructate it down?

TIA
Dale

Vladdy
02-10-2004, 02:40 PM
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/date.html#1194314

ellisd5
02-11-2004, 10:17 AM
Im still having problems with this

My current function is this


function isDate(sDateField) {
var selectedDate = new Date(sDateField.value);
var todaysDate = new Date();
todaysDate.setHours(0);
todaysDate.setMinutes(0);
todaysDate.setSeconds(0);
}

And its called here:


<INPUT TYPE="text" NAME="PAIRS_REQD_BY" CLASS="searchBox" onChange="isDate(this);">
<a href="javascript:show_calendar('SW_FORM.PAIRS_REQD_BY',null,null,'DD\/MM\/YYYY');"><img src="/btwportal/framework/skins/bt_wholesale/images/calender.gif" ALT="Calender" border="0" width="18" height="17"/></a>

The first problem is that I need to make sure that its in the correct format dd/mm/yyyy.

The second is that I dont think that im converting the inputed value into a proper date as it wont let me do comparissons like what is greater than the other.

And my third problem is that you can click the image next to it and bring up a calendar which sets the value in that textbox. I am unable to change the code in this but I still need to check that the date is today or in the future when I change the value using this.

TIA
Dale

Vladdy
02-11-2004, 12:01 PM
just create a new date and then make sure its value is a number, if not that means the string you supplied to Date constructor was incorrect:

selectedDate = new Date(selectedDateStr);
if(isNaN(selectedDate.valueOf()))
{ /* Process Error and return */
}
(todaysDate = new Date()).setHours(0,0,0,0); /*This can be one line of code, not 5 (you forgot to 0 milliseconds) */

ellisd5
02-11-2004, 01:50 PM
This still does not solve the problem :-


function isDate(sDateField) {
var selectedDate = new Date(sDateField.value);
var todaysDate = new Date();
todaysDate.setHours(0,0,0,0);
if (isNaN(selectedDate.valueOf())) {
alert("Not a valid date");
} else {
if (todaysDate > selectedDate) {
alert("Yea");
} else {
alert("Nope");
}
}
}

This is the currecnt code.
It doesn't like the this line


if (todaysDate > selectedDate) {


Which suggestd to me that is not a proper date, even though it didn't error when its set.
Also things like selectedDate.getFullYear() return what you would expect. So although its a date object, its sort of not :confused:

Can anyone solve my problem?

Vladdy
02-11-2004, 02:32 PM
use todaysDate.valueOf() > selectedDate.valueOf()

ellisd5
02-11-2004, 03:18 PM
Thanks Vladdy but its still not working but just figgured out why is not working!

The xml parser thats creating the html/javascript has replaced my > with &gt; which is not working :mad:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum