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

2022-01-18 00:00:00 input tags javascript html hidden

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

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">

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

How can I add a formatted date to the VALUE attribute?

推荐答案

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

    document.querySelector('.date').textContent = output; 

相关文章