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

D

dlx_son

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.
 
S

Samir

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>
 
R

RobG

Samir wrote:
[...]

<form action="">

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

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

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>
 
R

RobG

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>
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Thu, 24 Feb 2005 13:10:14, seen in
Samir said:
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?
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 25 Feb
2005 03:12:53, seen in RobG
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 25 Feb
2005 03:31:32, seen in RobG
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.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top