Problem with date calculation in form

P

Pete

Hi Guys
I have a form which must calc the difference between 2 date fields and
return the result in a third field. I have the following code but it does
not seem to work. Can anyone tell this total newbie where he is going wrong
or suggest a more elegant way of doing this.

thanks
Pete

<head><script>
function doit(oForm, usrInp)
{
var one_day = 1000 * 60 * 60 * 24 ;

var userDate = new Date(usrInp)
var dueDate = new Date(usrInp)

diff = dueDate - userDate
oForm.diff.value = (diff/one_day) + ' days';
}

//-->
</script>
</head>
<body>
<form name="a">
<input type="text" name="userDate" >
<input type="text" name="dueDate" onblur='doit(this.form, this.value)'>
<input type="text" name="diff" onfocus=this.blur();>
</form>
 
L

Lasse Reichstein Nielsen

Pete said:
Hi Guys
I have a form which must calc the difference between 2 date fields and
return the result in a third field. I have the following code but it does
not seem to work. Can anyone tell this total newbie where he is going wrong
or suggest a more elegant way of doing this.

thanks
Pete

<head><script>
function doit(oForm, usrInp)
{
var one_day = 1000 * 60 * 60 * 24 ;

var userDate = new Date(usrInp)

You mean:
var userDate = new Date(oForm.elements['userDate'].value);
var dueDate = new Date(usrInp)

diff = dueDate - userDate
oForm.diff.value = (diff/one_day) + ' days';

You might want to round to the nearest integer day. If the two dates
are one either side of a daylight saving time change, there will not
be an integer number of 864E5 milliseconds between them. So do:

var days = Math.round(diff/one_day);
oForm.diff.value = days + "day" + (days!=1?"s":"");

/L
 
P

Pete

Lasse
Thanks very much.

2 weeks I have been battling with this and you solved it in 20 minutes, and
on new years eve at that.

Pete


Lasse Reichstein Nielsen said:
Pete said:
Hi Guys
I have a form which must calc the difference between 2 date fields and
return the result in a third field. I have the following code but it does
not seem to work. Can anyone tell this total newbie where he is going wrong
or suggest a more elegant way of doing this.

thanks
Pete

<head><script>
function doit(oForm, usrInp)
{
var one_day = 1000 * 60 * 60 * 24 ;

var userDate = new Date(usrInp)

You mean:
var userDate = new Date(oForm.elements['userDate'].value);
var dueDate = new Date(usrInp)

diff = dueDate - userDate
oForm.diff.value = (diff/one_day) + ' days';

You might want to round to the nearest integer day. If the two dates
are one either side of a daylight saving time change, there will not
be an integer number of 864E5 milliseconds between them. So do:

var days = Math.round(diff/one_day);
oForm.diff.value = days + "day" + (days!=1?"s":"");

/L
 
P

Pete

Hi Lasse

I don't want to be a pain or to hassle you but do you know how I can get it
to accept dates in the dd/mm/yy format.
Thanks in advance
Pete


Lasse Reichstein Nielsen said:
Pete said:
Hi Guys
I have a form which must calc the difference between 2 date fields and
return the result in a third field. I have the following code but it does
not seem to work. Can anyone tell this total newbie where he is going wrong
or suggest a more elegant way of doing this.

thanks
Pete

<head><script>
function doit(oForm, usrInp)
{
var one_day = 1000 * 60 * 60 * 24 ;

var userDate = new Date(usrInp)

You mean:
var userDate = new Date(oForm.elements['userDate'].value);
var dueDate = new Date(usrInp)

diff = dueDate - userDate
oForm.diff.value = (diff/one_day) + ' days';

You might want to round to the nearest integer day. If the two dates
are one either side of a daylight saving time change, there will not
be an integer number of 864E5 milliseconds between them. So do:

var days = Math.round(diff/one_day);
oForm.diff.value = days + "day" + (days!=1?"s":"");

/L
 
L

Lasse Reichstein Nielsen

Pete said:
I don't want to be a pain or to hassle you but do you know how I can get it
to accept dates in the dd/mm/yy format.

Don't. That notation is so ambiguous, especially in an international
forum, that 03/04/05 can reasonably be expected to be read as any of
3rd of April 2005, 4th of March 2005 or 5th of April 2003. It is
better to train your users to write unambiguous formats like
2003-12-31. :)

Now, if you (or someone you work for :) insist on using dd/mm/yy, then
I suggest parsing it yourself instead of using the Date constructor.

Try:

---
function parseDate(string) {
// test that format is two digits - slash - two digits - slash - two digits
var match = string.match(/^(\d{2})\/(\d{2})\/(\d{2})$/);
if (!match) {return null;} // incorrect format.

var year = Number(match[3]);
if (year<70) { // 72 -> 1972, but 05 -> 2005
year += 2000;
} else {
year += 1900;
}
var month = Number(match[2])-1; // Date uses 0=January.
var date = Number(match[1]);

var theDate = new Date(year,month,date);
if (theDate.getDate()!=date || theDate.getMonth()!=month) {
return null; // illegal date, e.g., date=32 or month=13
}

return theDate;
}
---

Good luck.

(please don't top post/remember to trim the quotes)
/L
 
P

Pete

Again thanks very much. This is for an internal database capture page only
used by one person so this format will be perfect.
Pete

Lasse Reichstein Nielsen said:
Pete said:
I don't want to be a pain or to hassle you but do you know how I can get it
to accept dates in the dd/mm/yy format.

Don't. That notation is so ambiguous, especially in an international
forum, that 03/04/05 can reasonably be expected to be read as any of
3rd of April 2005, 4th of March 2005 or 5th of April 2003. It is
better to train your users to write unambiguous formats like
2003-12-31. :)

Now, if you (or someone you work for :) insist on using dd/mm/yy, then
I suggest parsing it yourself instead of using the Date constructor.

Try:

---
function parseDate(string) {
// test that format is two digits - slash - two digits - slash - two digits
var match = string.match(/^(\d{2})\/(\d{2})\/(\d{2})$/);
if (!match) {return null;} // incorrect format.

var year = Number(match[3]);
if (year<70) { // 72 -> 1972, but 05 -> 2005
year += 2000;
} else {
year += 1900;
}
var month = Number(match[2])-1; // Date uses 0=January.
var date = Number(match[1]);

var theDate = new Date(year,month,date);
if (theDate.getDate()!=date || theDate.getMonth()!=month) {
return null; // illegal date, e.g., date=32 or month=13
}

return theDate;
}
---

Good luck.

(please don't top post/remember to trim the quotes)
/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Lines: 53
Hi Lasse

I don't want to be a pain or to hassle you but do you know how I can get it
to accept dates in the dd/mm/yy format.

It already does that; it just doesn't give the right answer.

S = "31/12/03"
new Date( S.replace(/(\d\d)\/(\d\d)\/(\d\d)/, "20$3/$2/$1") )

works for dates in 2000..2099

S = "2003/12/31"

should need no adjustment, and should be substantially compliant with
any ZA Standard which derives from ISO 8601.


The following seems to window into 1970-2069 :

S = "31/12/63"
D = new Date( S.replace(/(\d\d)\/(\d\d)\/(\d\d)/, "$2/$1/$3") )
Q = 864E5*36525
D = new Date((+D+2*Q)%Q)

but cannot reasonably be extended to later periods.


Read the newsgroup FAQ.

(1) It will indicate how to format newsgroup replies, by quoting what
needs to be quoted and responding after.

(2) It will lead you to much of the information that
(a) (i) you have been asking for
(ii) you probably will be asking for
(b) (1) you need
(ii) you probably will need
on
(A) Dates
(B) Other things.

See below.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top