Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current formatted date dd/mm/yyyy in Javascript and append it to an input [duplicate]

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?

like image 829
VolosBlur Avatar asked Sep 13 '12 15:09

VolosBlur


People also ask

What date format is dd mm yyyy in JavaScript?

To format a date as dd/mm/yyyy: Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 .

How do you change the input type date in dd mm yyyy format?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.

How convert dd mm yyyy string to date in JavaScript?

To convert a dd/mm/yyyy string to a date: Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.


6 Answers

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?

like image 150
Aelios Avatar answered Oct 06 '22 04:10

Aelios


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

like image 20
Ali Avatar answered Oct 06 '22 06:10

Ali


<input type="hidden" id="date"/>
<script>document.getElementById("date").value = new Date().toJSON().slice(0,10)</script>
like image 24
Varun Natraaj Avatar answered Oct 06 '22 05:10

Varun Natraaj


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;
like image 21
Wutz Avatar answered Oct 06 '22 06:10

Wutz


By using the value attribute:

var today = new Date();
document.getElementById('DATE').value += today;
like image 25
Asciiom Avatar answered Oct 06 '22 05:10

Asciiom


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)

like image 34
Bart Friederichs Avatar answered Oct 06 '22 06:10

Bart Friederichs