473,383 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 2140
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raghu | last post by:
Hello... I am running into a problem while running a query..can some1 help.. this is the query : ************** SELECT * from Table S where S.dtDate1 BETWEEN...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
1
by: Greg | last post by:
I am using this calculation in a query to find invoices that are less then 30 days, over 30, over 60, over 90, and over 120. ...
5
by: David B | last post by:
I have a number of queries, running one after the other, which do quite a complex calculation. A text box on a form provides the date for this routine. I have another routine I wish to do and it...
2
by: David | last post by:
Hi, The data I am trying to print on the web looks as follows: Date 1 Record 1 Record 2 Record 3
5
by: jupiter | last post by:
hi friends, I am anil. I have begining level experience of c++ so I need ur advice. I have a problem for all think tanks in the group. what i want is 1. Access a web page (html) say ebay's...
4
meLady
by: meLady | last post by:
Hello, I have a form called feedback with a subform called feedback Options. I did some counting calculation in master form "feedback" at the bottom page. the calculation is about count the...
6
by: awojciehowski | last post by:
I am working with a database now and need some guidance. I am attempting to input an auto calculation. I have the ability to enter a date in a text box on a form. I want to be able to have...
7
by: plumba | last post by:
Hiya all. I have a form which the user selects a date from a javascript style calendar, it puts the date into a field in the format dd/mm/yyyy. I want the form to compare this user selected...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.