小编典典

如何在Javascript中获取当前格式的日期dd / mm / yyyy并将其附加到输入中

javascript

我想将当前日期添加到隐藏的HTML标记中,以便可以将其发送到服务器:

<input type="hidden" id="DATE" name="DATE" value="WOULD_LIKE_TO_ADD_DATE_HERE">

如何将格式化的日期添加到VALUE属性?


阅读 348

收藏
2020-04-25

共1个答案

小编典典

const monthNames = ["January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"];
        let dateObj = new Date();
        let month = monthNames[dateObj.getMonth()];
        let day = String(dateObj.getDate()).padStart(2, '0');
        let year = dateObj.getFullYear();
        let output = month  + '\n'+ day  + ',' + year;

        document.querySelector('.date').textContent = output;
2020-04-25