430

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?

gurghet
  • 7,591
  • 4
  • 36
  • 63
VolosBlur
  • 4,363
  • 3
  • 14
  • 9
  • 4
    First thing: don't mix Java with Javascript (don't even split the words Java and Script!). They are completely different languages. – Gilberto Torrezan Sep 13 '12 at 15:07
  • Do you need the client's local date? Could be an option to use the server's date? – gparis Sep 13 '12 at 15:11
  • 54
    `(new Date()).toLocaleDateString('en-GB')` is literally all you need to get the UK format date as per OP's question. – BadHorsie Aug 01 '18 at 17:26
  • `new Date().toLocaleDateString().padStart(10, '0')` – iPzard Sep 19 '22 at 00:34
  • Don't do this. The server knows the current date and time and should not trust the value sent by the client because it is trivial to modify the value of a hidden input. – Paul Apr 15 '23 at 15:46

7 Answers7

694

I hope this is what you want:

const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();

if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;

const formattedToday = dd + '/' + mm + '/' + yyyy;

document.getElementById('DATE').value = formattedToday;

How do I get the current date in JavaScript?

shareef
  • 9,255
  • 13
  • 58
  • 89
Aelios
  • 11,849
  • 2
  • 36
  • 54
248

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:

<script>
$(document).ready(function() {

     // set an element
     $("#date").val( moment().format('MMM D, YYYY') );

     // set a variable
     var today = moment().format('D MMM, YYYY');

});
</script>

Use following chart for date formats:

enter image description here

Chris Boylan
  • 314
  • 4
  • 15
Ali
  • 5,021
  • 4
  • 26
  • 45
150
<input type="hidden" id="date"/>
<script>document.getElementById("date").value = new Date().toJSON().slice(0,10)</script>
Varun Natraaj
  • 7,402
  • 2
  • 18
  • 19
4

To get current date/time in javascript:

var date = new Date();

If you need milliseconds for easy server-side interpretation use

var value = date.getTime();

For formatting dates into a user readable string see this

Then just write to hidden field:

document.getElementById("DATE").value = value;
Community
  • 1
  • 1
Wutz
  • 2,246
  • 13
  • 15
2

By using the value attribute:

var today = new Date();
document.getElementById('DATE').value += today;
Asciiom
  • 9,867
  • 7
  • 38
  • 57
1

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)

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
-1

You edit an element's value by editing it's .value property.

document.getElementById('DATE').value = 'New Value';
gen_Eric
  • 223,194
  • 41
  • 299
  • 337