This question already has an answer here:
- How do I get the current date in JavaScript? 41 answers
I would like to add a current date to a hidden HTML tag so that it can be sent to the server:
<input type="hidden" id="DATE" name="DATE" value="WOULD_LIKE_TO_ADD_DATE_HERE">
How can I add a formatted date to the VALUE attribute?
Use the DOM's
getElementByid
method:document.getElementById("DATE").value = "your date";
A date can be made with the
Date
class:d = new Date();
(Protip: install a javascript console such as in Chrome or Firefox' Firebug extension. It enables you to play with the DOM and Javascript)
By using the value attribute:
You edit an element's
value
by editing it's.value
property.I honestly suggest that you use moment.js. Just download
moment.min.js
and then use this snippet to get your date in whatever format you want:Use following chart for date formats:
To get current date/time in javascript:
If you need milliseconds for easy server-side interpretation use
For formatting dates into a user readable string see this
Then just write to hidden field: