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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date comparison problem, please look at my function

Status
Not open for further replies.

mat41

Programmer
Mar 7, 2004
91
AU
Good day. I am having trouble with:

if(document.assessAdmin.warningDate.value > periodFinishDate)
{
alert("Your 'Warning Date' value(" + document.assessAdmin.warningDate.value +") must be less than the end date of the current period(" + periodFinishDate + ") ");
document.assessAdmin.warningDate.focus();
document.assessAdmin.warningDate.select();
return(false);
}

The condition does not get entered in to. I have been looking at it for far to long. It does not error. I placed the alert outside the condition to see the values, they are:

document.assessAdmin.warningDate.value = 27/10/2006
periodFinishDate = 30/9/2006

FYI: the periodFinishDate value gets passed during form validation inside an event in an image tag. When I view the page source the function shows the following values (they are the start and the end dates of the current FY quarter):

onClick="return val('1/7/2006','30/9/2006');"

My objective is to not allow the form to submit if the user defined warning date is > than the last day of the current FY quarter.

TYIA
 
you need to cast the dates as dates first - right now they're just strings.

try this:

Code:
var d1 = new Date(document.assessAdmin.warningDate.value);
var d2 = new Date(periodFinishDate);

if(d1 > d2) {
    alert("Your 'Warning Date' value(" + document.assessAdmin.warningDate.value  +") must be less than the end date of the current period(" + periodFinishDate + ") ");
    document.assessAdmin.warningDate.focus();
    document.assessAdmin.warningDate.select();
    return(false);
}



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
that doesnt seem to work, I tried:

alert("one");
var d1 = new Date(document.assessAdmin.warningDate.value);
var d2 = new Date(periodFinishDate);
if(d1 > d2)
{
alert("two");
alert("ppYour 'Warning Date' value(" + document.assessAdmin.warningDate.value +") must be less than the end date of the current period(" + periodFinishDate + ") ");
return(false);
}
alert("three");

Alert on and three show however two does not. The warning date set is definantly > than the lock date. I have been trying to get this working for so long, any other suggestions?
 
They are both in aussie format:
dd/mm/yyyy

When you say 'compensate for format' what do you mean? Shouldnt I be able to compare dates which are the same format?
 
you alerted the dates after converting them to date objects, and all they showed was dd/mm/yyyy?

i'd expect to see something like: [tt]Mon Aug 28 23:08:48 EDT 2006[/tt].



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
applogies is does indicate that format. I have just noticed the date 3/10/2006 (3 oct) is being intepreted as:

fri mar 10....... (in the format you suggested)

Can I not compare dd/mm/yyyy formats. Do they need to converted to mm/dd/yyyy to be compared?

I appreciate your help very much
 
I guess so... that's kind of a bummer.

try something like this:

Code:
var date1Whole = '3/10/2006';
var date1Parts = date1Whole.split("/");

var d1 = date1Parts[0];
var m1 = date1Parts[1];
var y1 = date1Parts[2];

var date2Whole = '3/12/2006';
var date2Parts = date2Whole.split("/");

var d2 = date2Parts[0];
var m2 = date2Parts[1];
var y2 = date2Parts[2];

var aussieDate1 = new Date(m1 + "/" + d1 + "/" + y1);
var aussieDate2 = new Date(m2 + "/" + d2 + "/" + y2);

then proceed as normally.

i'm going to ask a guy in the UK how he does this -- this seems ridiculous. i thought it was a browser setting thing that JS could interpret.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thank you for your reply I am going to try this now. Reallt appreciate your help. I am an ASP guy however would rather achieve this validation on the client side

TYVM
 
cLFlaVA Thanking you. The following method works great, you have a fine day!!

var AUwarningDate = document.assessAdmin.warningDate.value;
var AUWarningDateParts = AUwarningDate.split("/");
var AUWarningD = AUWarningDateParts[0];
var AUWarningM = AUWarningDateParts[1];
var AUWarningY = AUWarningDateParts[2];
var USWarningDate = new Date(AUWarningM + "/" + AUWarningD + "/" + AUWarningY);
 
a suggestion:

to control the format a date is entered into a field, i suggest using a date picker. you can find several examples of these online. this way, a user can't mess things up by entering:

12-3-06
12-03-2006
12/3/06
12.3.06

etc...



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I use a date picker. It has onFocus and on click 'im sorry you must use clcik the date picker image' the user can not manually type dates. The date picker selects and shows dd/mm/yyyy type format.

I use this validation to ensure the waarnig date (warns the assessment input area assessments will lock out in X amoubt of days) and a freeze date (Lock out assessment input)

Therfore I use JS to validate:
1..Is the warningDate < the FreezeDate AND > the period (Financial Year quarter) start date AND < the period finish date

2..Is the freeze date > than the warning Date AND > than the period start date AND < the period finish date

FYI: This is an ASP application, dates do get inserted into SQL Server in US format.
 
Me to. My JS is average, this forum always delivers. Very grateful for your assistance, you have a fine day!!
 
Not sure if anyone's mentioned this, but you actually want to compare them as yyyy/mm/dd. Otherwise Jan 1 2007 will be before Dec 31 2006!
 
You are kidding? I am so used to tweaking dates to suit me with server side code I failed to realize client side stuff should conform to the international format you suggest, mmmmmmm.

Thanks for the heads up. I should always use yyyy/mm/dd for client side comparisons?
 
Miros, can you show an example of how that happens?

the code i provided above works as expected, with Dec 31, 2006 showing properly BEFORE Jan 1, 2007:

Code:
<script type="text/javascript">
<!--

var date1Whole = '31/12/2006';
var date1Parts = date1Whole.split("/");

var d1 = date1Parts[0];
var m1 = date1Parts[1];
var y1 = date1Parts[2];

var date2Whole = '1/1/2007';
var date2Parts = date2Whole.split("/");

var d2 = date2Parts[0];
var m2 = date2Parts[1];
var y2 = date2Parts[2];

var aussieDate1 = new Date(m1 + "/" + d1 + "/" + y1);
var aussieDate2 = new Date(m2 + "/" + d2 + "/" + y2);

if ( aussieDate1 < aussieDate2 ) alert( aussieDate1 + " is before " + aussieDate2 );
else alert(aussieDate1 + " is after " + aussieDate2);

//-->
</script>



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top