Home >Web Front-end >JS Tutorial >How can I display the current date in the input box?
Go directly to the code:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>input框中自动展示当前日期</title> </head><body><input type="text" id="input"><script> //获取input元素var _input = document.getElementById('input'); var date = new Date(); var seperator = "/";var year = date.getFullYear(); var month = date.getMonth() + 1;var strDate = date.getDate();if (month >= 1 && month <= 9) { month = "0" + month; }if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; }var currentDate = year + seperator + month + seperator + strDate; //当前日期_input.value = currentDate; //赋值给input.value _input.setAttribute('disabled', 'disabled'); //不可编辑</script></body></html>
Note here that the getMonth() method uses local time. The return value is an integer between 0 (January) and 11 (December).
The above is the detailed content of How can I display the current date in the input box?. For more information, please follow other related articles on the PHP Chinese website!