472,119 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Problem with date calculation in form

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>


Jul 20 '05 #1
6 2068
"Pete" <pe**@pete.com> writes:
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
--
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
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" <lr*@hotpop.com> wrote in message
news:is**********@hotpop.com...
"Pete" <pe**@pete.com> writes:
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
--
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 #3
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" <lr*@hotpop.com> wrote in message
news:is**********@hotpop.com...
"Pete" <pe**@pete.com> writes:
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
--
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
"Pete" <pe**@pete.com> writes:
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
--
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 #5
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" <lr*@hotpop.com> wrote in message
news:d6**********@hotpop.com...
"Pete" <pe**@pete.com> writes:
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
--
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 #6
JRS: In article <MY********************@is.co.za>, seen in
news:comp.lang.javascript, Pete <pe**@pete.com> posted at Wed, 31 Dec
2003 19:44:32 :-

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.

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:is**********@hotpop.com...
"Pete" <pe**@pete.com> writes:
> Hi Guys
> I have a form which mus > ... ...


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Richard Hollenbeck | last post: by
5 posts views Thread by David B | last post: by
5 posts views Thread by jupiter | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.