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 15:10

I hope this is what you want:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!

var yyyy = today.getFullYear();
if (dd < 10) {
  dd = '0' + dd;
} 
if (mm < 10) {
  mm = '0' + mm;
} 
var today = dd + '/' + mm + '/' + yyyy;
document.getElementById('DATE').value = today;

How do I get the current date in JavaScript?

查看更多
登录 后发表回答