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>