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

Date calculator, add or subtract days to or from a given date

P: n/a
Here is the code so far

<form name="thisform">

<h3>Enter time to add to or subtract from:</h3>
(If not entered, current time will be used)<br>

Day: <input name="d1" alt="Day of month" size=3>
Month: <input name="m1" alt="Month" size=3>
Year: <input name="y1" alt="Year" size=5> (4 digits for year, e.g.
1950)<br>
Hours: <input name="h1" alt="Hours" size=3>

Minutes: <input name="i1" alt="Minutes" size=3>
Seconds: <input name="s1" alt="Seconds" size=3>
<h3>Enter time to add/subtract</h3>
<select name="type">
<option value="add">Add
<option value="sub">Subtract
</select>
<input alt="Amount to add/subtract" name=amount size=10>
<select name="unit">
<option value="d">days
<option value="h">hours
<option value="m">minutes
<option value="s">seconds
</select>

(Note: Value to add or subtract must currently be less than 300
years)<br>
<input type=submit alt="Calculate" value="Calculate new date!">
</form>

I would like to click on the submit button and then some kind of alert
window would pop up. Or just write it on the page.

thanks.

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Lee
dl*****@yahoo.com said:

Here is the code so far I would like to click on the submit button and then some kind of alert
window would pop up. Or just write it on the page.


What class is this for?

Jul 23 '05 #2

P: n/a
It's not for a class,

Here's the Original Code, I am trying to change to use in a more useful
form for me, and ebay too.

<form>
<input type="text" name="date1" size="20"><br>
date (yyyy/mm/dd)<br><br>
<input type="text" name="date2" size="20" readonly><br>
90 days from date<br><br>
<input type="button" value="Calculate"
onclick="plus90(this);">
</form>

<script type="text/javascript">

document.forms(0).date1.value = dateToString(new Date())

function dateToString(d){
return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')

}

function plus90(x){
d = new Date(x.form.date1.value)
d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)
x.form.date2.value = dateToString(d)

}

</script>

Jul 23 '05 #3

P: n/a
Samir wrote:
[...]
<form>
<form action="">

[...]
document.forms(0).date1.value = dateToString(new Date())
document.forms[0].date1.value = dateToString(new Date())
--------------^-^

<URL:http://www.jibbering.com/faq/faq_notes/square_brackets.html>

function dateToString(d){
return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')
getYear() will return the year from 1900 (unless you are using
IE, which does not properly implement getYear). You should
either use getFullYear() or for wider support, create your own
full year function:

return [
myFullYear(d.getYear()),
d.getMonth()+1,
d.getDate()
].join('/')

function myFullYear(y){
return (y<1900)?y+1900:y;
}
}

function plus90(x){
d = new Date(x.form.date1.value)
d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)


Why generate an entirely new date?

function plus90(x){
d = new Date(x.form.date1.value)
d.setDate(d.getDate()+90);

[...]

given the above, subtracting days should be simple easy. As for
time, you should convert the change in hours, minutes and
seconds to milliseconds, then use:

var changeInMilliseconds = 1000 * (
hoursEntered *60*60
+ minutesEntered *60
+ secondsEntered * 1);

d.setTime(d.getTime() + changeInMilliseconds);
secondsEntered needs to be converted from a string to number,
hence *1, which is a nice way of doing it in sync with the other
variables.

Using milliseconds this way to adjust the time is not perfect,
but pretty darn close for short time intervals. I've also
tossed in a function to add leading zeros to single digit
numbers in the date string. Full code below.
--
Rob

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Time play</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">

<script type="text/javascript">
function dateToString(d){
var dateString = myFullYear(d.getYear())
+ '/' + addZ(d.getMonth()+1)
+ '/' + addZ(d.getDate())
+ ' ' + addZ(d.getHours())
+ ':' + addZ(d.getMinutes())
+ ':' + addZ(d.getSeconds())
return dateString;
}

function myFullYear(y){
return ( y < 1900 )? y + 1900 : y;
}

function plus90(x){
d = new Date(x.form.date1.value)
d.setDate(d.getDate()+90);
x.form.date2.value = dateToString(d);
document.getElementById('msgBox').innerHTML = dateToString(d);
}

function addTime(x){
var tBit = x.form.timeChange.value.split(':');
var changeInMilliseconds = 1000 * (
tBit[0] *60*60
+ tBit[1] *60
+ tBit[2] * 1);

d.setTime(d.getTime() + changeInMilliseconds);
x.form.date2.value = dateToString(d);
document.getElementById('msgBox').innerHTML = dateToString(d);
}

function addZ(z) {
return (z<10)? '0' + z : z;
}

</script>

</head>

<body>
<form action="">
<input type="text" name="date1" size="20"><br>
date (yyyy/mm/dd)<br><br>
<input type="text" name="date2" size="20" readonly><br>
90 days from date<br>
<input type="text" name="timeChange" size="20"
value="03:12:37">
<br>
<input type="button" value="Add 90 days"
onclick="plus90(this);">
<br>
<input type="button" value="Add time"
onclick="addTime(this);">
<br>
<input type="reset">
</form>
<span id="msgBox"></span>

<script type="text/javascript">
document.forms[0].date1.value = dateToString(new Date())
</script>

</body>
</html>
Jul 23 '05 #4

P: n/a
RobG wrote:
[...]
function myFullYear(y){
return (y<1900)?y+1900:y;
}


Ooops, the above works fine in Mozilla-based browsers for all
relevant (post 1 AD) years, however it will fail in IE for dates
below 1900. A better (but not perfect) myFullYear is:

function myFullYear(y){
return ( y < 999 )? y + 1900 : y;
}

This still works fine in the 'Zillas, but will give erroneous
results in IE for years before 999. Presumably any usage related
to eBay does not require coverage for this time frame. :-x

For further information, read here:

<URL:http://www.merlyn.demon.co.uk/js-date0.htm#gFY>

--
Rob
Jul 23 '05 #5

P: n/a
JRS: In article <11*********************@g14g2000cwa.googlegroups. com>,
dated Thu, 24 Feb 2005 13:10:14, seen in news:comp.lang.javascript,
Samir <dl*****@yahoo.com> posted :
Here's the Original Code, I am trying to change to use in a more useful
form for me, and ebay too.

<form>
<input type="text" name="date1" size="20"><br>
date (yyyy/mm/dd)<br><br>
<input type="text" name="date2" size="20" readonly><br>
90 days from date<br><br>
<input type="button" value="Calculate"
onclick="plus90(this);">
</form>

<script type="text/javascript">

document.forms(0).date1.value = dateToString(new Date())

function dateToString(d){
return [d.getYear(),d.getMonth()+1,d.getDate()].join('/')

}

function plus90(x){
d = new Date(x.form.date1.value)
d = new Date(d.getYear(),d.getMonth(),d.getDate()+90)
x.form.date2.value = dateToString(d)

}

</script>

You should validate the input date. In a Murrican-infested world, you
must expect to get dates written as 2/25/05, whatever you ask for.
Validation can return a Date Object. But perhaps you have not yet
bothered to read the newsgroup FAQ with care? See sig.

Function dateToString should give YYYY-MM-DD, since that is the
standard. So you should use a leading zero function. I doubt whether
it is worth creating an array and then joining it; I would use
with (d)
return getFullYear()+'-'+LZ(getMonth()+1)+'-'+LZ(getDate())

Input should match that, so use .replace(/-/g, "/") or similar; but,
in combining getting an Object with validation, that becomes irrelevant.

It is strange that you have learned that new Date() accepts
extended-range fields but are not using setDate() in that manner -
d.setDate(d.getDate()+90)

Better, IMHO, to pass this.form into plus90()

Better to use a named form rather than relying on it being forms(0)
- should that be forms[0] anyway?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of 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 23 '05 #6

P: n/a
JRS: In article <VI**************@news.optus.net.au>, dated Fri, 25 Feb
2005 03:12:53, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :

function plus90(x){
d = new Date(x.form.date1.value)
d.setDate(d.getDate()+90);
That gives the same local time (if it exists; 1 chance in 8766 currently
that it does not) 90 days on.

given the above, subtracting days should be simple easy. As for
time, you should convert the change in hours, minutes and
seconds to milliseconds, then use:

var changeInMilliseconds = 1000 * (
hoursEntered *60*60
+ minutesEntered *60
+ secondsEntered * 1);

d.setTime(d.getTime() + changeInMilliseconds);


That moves ahead by a certain absolute interval.

If the hours entered are 90*24 = 2160, then there is about a 50% chance,
in many locations, that it will not give the same result as plus90.

The OP should be aware of this.

--
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 23 '05 #7

P: n/a
JRS: In article <o_**************@news.optus.net.au>, dated Fri, 25 Feb
2005 03:31:32, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.auau> posted :
A better (but not perfect) myFullYear is:

function myFullYear(y){
return ( y < 999 )? y + 1900 : y;
}

For current dates, return 2000 + y%100 works with all three
behaviours of getYear() that I've heard of.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of 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 23 '05 #8

P: n/a

Lu********@hotmail.com
*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.