Click to See Complete Forum and Search --> : Form date


byrddog
06-19-2003, 11:46 AM
Does anyone know how a form field can automatically have the current date in it in the format mm/dd/yyyy

Thanks
Brad

AdamBrill
06-19-2003, 12:10 PM
Like this:<script language=javascript type="text/javascript">
TheDate = new Date();
document.write('<input type=text value="'+((TheDate.getMonth().length<2)?"":"0")+(TheDate.getMonth()+1)+"/"+TheDate.getDate()+"/"+TheDate.getYear()+'">');
</script>

pyro
06-19-2003, 12:13 PM
Here it is in MM/DD/YYYY format:

<script language="JavaScript" type="text/JavaScript">

today = new Date();

day = today.getDate();
month = today.getMonth();
month++;
year = today.getFullYear();

if (day < 10) {
day = "0"+day;
}

if (month < 10) {
month = "0"+month;
}

alert (month+"/"+day+"/"+year);

</script>

byrddog
06-19-2003, 12:18 PM
How do I get it to automatically post to a form field?

Testing at:
www.massmusic.net/testing.html


Thanks
Brad

AdamBrill
06-19-2003, 12:20 PM
Did you look at my code?? Isn't that what you wanted?

pyro
06-19-2003, 12:22 PM
Try it like this:

<html>
<head>
<script language="JavaScript" type="text/JavaScript">

function setDate(){
today = new Date();

day = today.getDate();
month = today.getMonth();
month++;
year = today.getFullYear();

if (day < 10) {
day = "0"+day;
}

if (month < 10) {
month = "0"+month;
}

document.formname.fieldname.value = month+"/"+day+"/"+year;
}
</script>
</head>
<body onload="setDate();">
<form name="formname">
<input type="text" name="fieldname">
</form>
</body>
</html>

byrddog
06-19-2003, 12:31 PM
Adam,

I accidentally took pyro's code, and not yours.

Thanks for the help!

Brad

AdamBrill
06-19-2003, 12:32 PM
Ah, ok. ;) You had me confused there. :D

Charles
06-19-2003, 12:40 PM
We can shorten up that JavaScript a bit and use valid HTML thus:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
Date.prototype.toString = function () {return [this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate(), this.getFullYear()].join('/')}

onload = function () {document.forms[0].date.value = new Date()}
// -->
</script>
<title>Example</title>
<form action="">
<div>
<label>Date<br><input type="text" name="date"></label>
</div>
</form>

AdamBrill
06-19-2003, 12:47 PM
I fail to see how that is shorter. ;) Also, you forgot to put in the head and body... :confused: Shouldn't it be like this?<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
Date.prototype.toString = function () {return [this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate(), this.getFullYear()].join('/')}

onload = function () {document.forms[0].date.value = new Date()}
// -->
</script>
<title>Example</title>
</head>
<body>
<form action="">
<div>
<label>Date<br><input type="text" name="date"></label>
</div>
</form>
</body>
</html>

Charles
06-19-2003, 12:54 PM
Originally posted by AdamBrill
I fail to see how that is shorter. ;) Also, you forgot to put in the head and body...
Sorry for the confusion, I was replying to Pyro's post.

1) The start and end tags for the HTML, HEAD and BODY elements are optional in HTML.

2) The "language" attribute of the SCRIPT element has ben depricated. And it will cause Netscape to behave in some non-standard ways.

3) Your method relies upon JavaScript and for the 13% of users who do not use JavaScript the date will not appear. The methods that Pyro and I have presented will allow those users to input the date.

AdamBrill
06-19-2003, 05:49 PM
Originally posted by Charles[b]
1) The start and end tags for the HTML, HEAD and BODY elements are optional in HTML.
Really? I didn't know that... I guess I learn something new every day. ;) I'll take your word for it, though, since I can't remember a time when you were wrong about something like this... :D :D

Originally posted by Charles[b]
2) The "language" attribute of the SCRIPT element has ben depricated. And it will cause Netscape to behave in some non-standard ways.
I could be wrong, but I thought it was best to put in both... That way it will work on older browsers. Does it mess up Netscape if you put both of them in there?

Originally posted by Charles[b]
3) Your method relies upon JavaScript and for the 13% of users who do not use JavaScript the date will not appear. The methods that Pyro and I have presented will allow those users to input the date.
LoL... When I got the e-mail saying you replied, I knew you would say that. ;) Is noscript depreciated or is it ok to use that?

Charles
06-19-2003, 06:10 PM
Originally posted by AdamBrill
Really? I didn't know that... I guess I learn something new every day. ;) I'll take your word for it, though, since I can't remember a time when you were wrong about something like this... :D :D

I could be wrong, but I thought it was best to put in both... That way it will work on older browsers. Does it mess up Netscape if you put both of them in there?

LoL... When I got the e-mail saying you replied, I knew you would say that. ;) Is noscript depreciated or is it ok to use that? 1) I'm wrong all of the time and often people on this board are kind enough to correct me. It was said of a teacher of mine, at his memorial service (and a memorial service for a philosopher is something else, I assure you) that he was truly happy to be found wrong about something. I'm at the point where I appreciate being found wrong but I'm not at the happy stage...yet. But the table at http://www.w3.org/TR/html4/index/elements.html lists all valid HTML elements and which tags are optional or forbidden.

2) With Netscape, "In JavaScript 1.2 and earlier versions, toString returns a string representing the source code of the array. This value is the same as the value returned by the toSource method in JavaScript 1.3 and later versions." (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/array.html#1193921). The problem is that Array.toString() can be called implicitly.

3) Using the NOSCRIPT element is a perfectly acceptable technique. It's just that I have a fondness for scripts that transition well without it.