compare dates.

A

alexis

Hi,

In a form I have the curent date
<input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>">
and
<input type=text name="datebox" size=15>
The date format is d/m/Y (day/month/year) in both case

Now my problem is that i want to know if 'datebox' is valid, that means
after 'datetoday'.

I tried :

function checkFields() {
missinginfo = "";
if (document.form.datebox.value < document.form.datetoday.value) {
missinginfo += "\n - date not valid";
}

But this doesn't work because of the '/'.

Can someone help me, thanks,

AL
 
L

Lasse Reichstein Nielsen

alexis said:
In a form I have the curent date
The date format is d/m/Y (day/month/year) in both case

I'll assume you'll have checked this, so we can trust the format.
You should consider that that date format is ambiguous, since people
in some coutries will read it as m/d/Y if they can.
Now my problem is that i want to know if 'datebox' is valid, that
means after 'datetoday'.
I tried :
if (document.form.datebox.value < document.form.datetoday.value) {

You are comparing them as strings.
But this doesn't work because of the '/'.

That's not the problem. If the format was YYYY/mm/dd, then string
comparison would work, because later dates would compare larger as
strings. It doesn't work for you, because you have the least
significant part of the date first, so "10/01/1970" is larger than
"09/12/2036", because "1" is larger than "0" and the rest is ignored.

To use string comparison, you *must* use the same number of digits
for each part every time, so the 3rd will be "/03", not "/3".
Can someone help me, thanks,

Either change the format, or parse the string.

Start with:
---
var today = document.forms['form'].elements['datetoday'].value;
var someday = document.forms['form'].elements['datebox'].value;
var todayParts = today.split("/");
var somedayParts = someday.split("/");
---
That gives you arrays where, e.g., "31/11/2003" becomes
["31","11","2003"].

You can now compare yourself or use the Date constructor:

---
if ( (Number(todayParts[2]) < Number(somedayParts[2])) ||
(Number(todayParts[2]) == Number(somedayParts[2]) &&
( (Number(todayParts[1]) < Number(somedayParts[1])) ||
( (Number(todayParts[1]) == Number(somedayParts[1])) &&
(Number(todayParts[0]) < Number(somedayParts[0])) ))))
{
// today is before someday.
}
---
or
--
var todayDate = Date.UTC(Number(todayParts[2]), //year
Number(todayParts[1])-1, //month
Number(todayParts[0]) //date
);
var somedayDate = Date.UTC(Number(somedayParts[2]), //year
Number(somedayParts[1])-1, //month
Number(somedayParts[0]) //date
);

if ( todayDate < somedayDate) {
// today < someday
}
 
S

Steve van Dongen

Hi,

In a form I have the curent date
<input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>">
and
<input type=text name="datebox" size=15>
The date format is d/m/Y (day/month/year) in both case

Now my problem is that i want to know if 'datebox' is valid, that means
after 'datetoday'.

I tried :

function checkFields() {
missinginfo = "";
if (document.form.datebox.value < document.form.datetoday.value) {
missinginfo += "\n - date not valid";
}

But this doesn't work because of the '/'.

var d1 = document.form.datebox.value;
var d2 = document.form.datetoday.value;
if (Date.parse(d1) < Date.parse(d2))
...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthparse.asp

You don't need to verify that the string entered by the user is a
valid date format before attempting to perform the comparison.

Regards,
Steve
 
L

Lasse Reichstein Nielsen

var d1 = document.form.datebox.value;
var d2 = document.form.datetoday.value;
if (Date.parse(d1) < Date.parse(d2))

My browser's Date.parse parses "09/10/2003" as month/day/year, so it
won't work.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in

That, of course, is not in accordance with an applicable European Norm.
Brussels is not always wrong.

I'll assume you'll have checked this, so we can trust the format.
You should consider that that date format is ambiguous, since people
in some coutries will read it as m/d/Y if they can.

And, as you have subsequently shown, browsers in more countries will.

Either change the format, or parse the string.

S = '23/04/03'
S = S.replace(/(\d+).(\d+).(\d+)/, '$3/$2/$1')
S = S.replace(/^(\d\d\/)/, '20$1')
D = new Date(S)

and compare Date Objects. Assumes D M Y, and D is d or dd, M is m or
mm, Y is yy or yyyy.

S = S.replace(/\b(\d)\b/g, '0$1') // inserts leading zeroes

and with leading zero and corrected (to ISO) order & yyyy one can
compare strings.

OP : Test carefully. Better to get dates in proper yyyy-mm-dd form in
the first instance.


Mote that if the date is first split into Y M D strings one can generate
a PseudoDate = (Y*100+ +M)*100+ +D and compare those directly; the
extra + signs and spaces are *vital*. H'mmm - +D+(+M+Y*100)*100 seems
OK too, & +D+100*(+M+100*Y) . PseudoDates can be compared but should
not be subtracted, except to obtain the sign.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top