ChatGPT解决这个技术问题 Extra ChatGPT

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

This question already has answers here: How do I get the current date in JavaScript? (60 answers) Closed 8 years ago.

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?

First thing: don't mix Java with Javascript (don't even split the words Java and Script!). They are completely different languages.
Do you need the client's local date? Could be an option to use the server's date?
(new Date()).toLocaleDateString('en-GB') is literally all you need to get the UK format date as per OP's question.

s
shareef

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?


For most uses, replace the last line with "return today;"
This is helpful, but confirms that Javascript is insanely inefficient at getting a formatted date returned. It's unfathomable to have to write more than one line of code to do this...
new Date(Date.now()).toLocaleString();
date.toLocaleDateString('en-GB')
new Date(Date.now()).toLocaleString().split(',')[0] for the current date in dd/mm/yyyy format only.
C
Chris Boylan

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:

https://i.stack.imgur.com/xVscR.jpg


simple and effective solution to all date display problems in JavaScript
I will definitely use moment.js from now on. Very easy and very effective to use. @Ali thanks a lot.
You can parse dates like this: moment('26/04/2016', 'DD/MM/YYYY').format('YYYY-MM-DD');
btw moment.js is NOT a small library
For those who want similar functionality with a smaller footprint, Day.js is excellent! npmjs.com/package/dayjs
V
Varun Natraaj
<input type="hidden" id="date"/>
<script>document.getElementById("date").value = new Date().toJSON().slice(0,10)</script>

d.toJSON().slice(0,10).split('-').reverse().join('/')
An alternative to @Gaurav's solution for those of us who need the date in ISO fomat: new Date().toJSON().slice(0,10).replace(/-/g,'/')
new Date(Date.now()).toLocaleString().split(',')[0]
date.toLocaleDateString('en-GB');
to format in dd/mm/yyyy new Date().toJSON().slice(0,10).split('-').reverse().join('/')
C
Community

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;

or just document.getElementById('DATE').value = (new Date()).getTime();
new Date().toJSON().slice(0,10).split('-').reverse().join('/') works good for me.
A
Asciiom

By using the value attribute:

var today = new Date();
document.getElementById('DATE').value += today;

B
Bart Friederichs

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)


g
gen_Eric

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

document.getElementById('DATE').value = 'New Value';