/    Sign up×
Community /Pin to ProfileBookmark

Comparing two dates on a form

Aloha!

I’ve been borrowing JS for over 7 years to use in an application I developed for work, and still to this day I have no clue what I’m doing. I’m hopeful that a kind JS veteran developer can help me out!

We have a form set up to record periods of days between two dates, and I just want to add JS to verify that the beginning date is not equal to or later than the end date (otherwise the days count would be 0 or negative, which would be nonsensical).

The trick is that the code I thought would work, below, is yielding in IE8 (the ONLY browser my company allows) an error of “Object doesn’t support this property or method”. Here’s the JS code for the date comparison:

[CODE]function dateCompare() {
var EndDate = document.getElementByID(‘End_Date’).value ;
var BeginDate = document.getElementByID(‘Begin_Date’).value ;
alert (‘BeginDate = ‘ + BeginDate + ‘ – EndDate = ‘ + EndDate) ;
return false;
if (EndDate = “”) {
if (EndDate <= BeginDate) {
alert(‘The end date cannot be the same or earlier as the begin date’);
return false;
}
}
}[/CODE]

I tried pulling the field value using document.forms.DateSelector.End_Date.value and if I do that, the code complains that “document.forms.DateSelector.End_Date’ is null or not an object”.

Perhaps, I’m theorizing, the problem is due to the use of the handy dandy calendar picker tool developed by Mihai Bazon and explained here: [url]http://www.dynarch.com/projects/calendar/old/[/url]

If anyone has ideas on what I could try to pull the date value from each field and compare the two to verify they are in the right order in time, I’d be very appreciative! I searched and found a huge amount of examples, and tried several, but the issue keeps coming up. I think it’s all about the form field values not pulling up.

Thanks very much in advance for the help!

Me ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyJan 25.2013 — Well first things first, 'getElementByID()' is not a valid javascript method. So if this isn't a typo, you will need to adjust those lines to use 'getElementById()' instead.
Copy linkTweet thisAlerts:
@NatuScapeauthorJan 25.2013 — Thank you! See what I meant about still not having a clue after 7 years of "writing" JS into my application? ? Anyway, I found the following code to work, although it's probably less elegant than it could be. Any feedback to make it nicer?

[CODE]function dateCompare() {
var EndDate = Date.parse(document.getElementById('End_Date').value) ;
var BeginDate = Date.parse(document.getElementById('Begin_Date').value) ;
alert ('BeginDate = ' + BeginDate + ' - EndDate = ' + EndDate) ;
if (isNaN(EndDate))
{
}
else
{
if (EndDate <= BeginDate) {
alert('The end date cannot be the same or earlier as the begin date');
return false;
}
}
}
[/CODE]


Thanks again, really appreciate it!!!

Me ?
Copy linkTweet thisAlerts:
@srinuettaJan 25.2013 — change the above code

if (!isNaN(EndDate)){

if (EndDate <= BeginDate) {
alert('The end date cannot be the same or earlier as the begin date');
return false;
}


}
Copy linkTweet thisAlerts:
@NatuScapeauthorJan 25.2013 — Thank you very much!!! ? I forgot that I knew that ! meant "not".

Lovely, thanks!

Me ?
×

Success!

Help @NatuScape spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...