By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

compare dates.

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
alexis <te**@test.com> writes:
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
}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
On Sun, 30 Nov 2003 12:43:13 +0100, alexis <te**@test.com> wrote:
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/de...jsmthparse.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
Jul 20 '05 #3

P: n/a
Steve van Dongen <st*****@hotmail.com> writes:
The date format is d/m/Y (day/month/year) in both case
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4

P: n/a
JRS: In article <he**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sun, 30 Nov 2003 13:19:57 :-
alexis <te**@test.com> writes:
In a form I have the curent date
The date format is d/m/Y (day/month/year) in both case


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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.