Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  89] [ 6]  / answers: 1 / hits: 191874  / 15 Years ago, tue, june 16, 2009, 12:00:00

Using javascript, how can we auto insert the current DATE and TIME into a form input field.



I'm building a Problems form to keep track of user submitted complaints about a certain service. I'm collecting as much info as possible. I want to save the staff here from having to manually enter the date and time each time they open the form.



I have this snippet of code:



<input id=date name=date value=javascript:document.write(Date()+'.')/>


but it's not working.



many thanks for any help.


More From » forms

 Answers
33

Javascript won't execute within a value attribute. You could do something like this, though:



<input id=date name=date>

<script type=text/javascript>
document.getElementById('date').value = Date();
</script>


You'd probably want to format the date as you prefer, because the default output of Date() looks something like: Tue Jun 16 2009 10:47:10 GMT-0400 (Eastern Daylight Time). See this SO question for info about formatting a date.


[#99305] Thursday, June 11, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
2 Years ago
janjadonb questions
;