Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prepopulating a form date field using javascript 2

Status
Not open for further replies.

cdumigan

MIS
Feb 5, 2002
16
0
0
IE
Hi there,
I'm trying to write a function that will prepopulate a form date field with the current (todays) date. However I'm not doing to well. Does anybody have or know of a function that will do this for me?
Thanks in advance,
C
 
you can do this very easily :

<html>
<head>
<title>Example</title>
</head>

<script>
function fillWithToday(formElm)
{
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth()+1;// + 1 because its an array
var day = today.getDay();

formElm.value = day + &quot;/&quot; + month + &quot;/&quot; + year
}
</script>

<body onload=&quot;fillWithToday(document.myForm.myDateField)&quot;>

<form name=myForm>
<input type=text name=myDateField>
</form>

</body>
</html>

Hope this helps. Gary Haran
 
Hi,

Gary's example give you 2/4/2002 where 2/ = the second day of the week, because today.getDay(); returns the day of the week [values between 0(Sunday) and 6 (Saturday))

To get the day of the months you have to use:
var day = today.getDate();

Sorry to improve you Gary ;-)
Erik

<!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top