给定一个输入元素:

<input type="date" />

有没有办法将日期字段的默认值设置为今天的日期?


当前回答

这依赖于PHP:

<input type="date" value="<?php echo date('Y-m-d'); ?>" />

其他回答

来匹配原始查询。

date.value = new Date().toJSON().split('T')[0] <输入类型=“日期” id=“日期”/>

如果你正在使用ruby,你可以使用这个来设置默认值为今天的日期和时间:

<input type="datetime-local" name="time" value="<%= Time.now.strftime('%Y-%m-%dT%H:%M') %>" />

这将返回与ISO相同的YYYY-MM-DD格式,但是您的本地时间,而不是UTC。

function getToday() {
    return new Date().toLocaleDateString('en-CA', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
    });
}

使用HTMLInputElement.prototype.valueAsDate:

document.getElementById('datePicker').valueAsDate = new Date();
new Date().getFullYear()+"-"+ ((parseInt(new Date().getMonth())+1+100)+"").substring(1)