How to get current formatted date dd/mm/yyyy in Ja

2019-01-01 14:38发布

This question already has an answer here:

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?

7条回答
爱死公子算了
2楼-- · 2019-01-01 14:48
<input type="hidden" id="date"/>
<script>document.getElementById("date").value = new Date().toJSON().slice(0,10)</script>
查看更多
永恒的永恒
3楼-- · 2019-01-01 14:49

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)

查看更多
路过你的时光
4楼-- · 2019-01-01 14:50

By using the value attribute:

var today = new Date();
document.getElementById('DATE').value += today;
查看更多
梦寄多情
5楼-- · 2019-01-01 14:50

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

document.getElementById('DATE').value = 'New Value';
查看更多
闭嘴吧你
6楼-- · 2019-01-01 15:00

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

查看更多
一个人的天荒地老
7楼-- · 2019-01-01 15:03

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;
查看更多
登录 后发表回答