Print

html 自动获取当前时间填充到input框内


作者: 文章来源:

本文原地址:https://www.mimiwuqi.com/webqianduan/196500.html

本文内容来源于最近项目开发中的一个需求,因为我也研究了老半天,终于实现该功能,所以特此整理记录一下。

这个需求要求将自动获取的当前时间填充到 input 框内,并且还可以修改时间,话不多说,直接上代码。

代码如下:

<div >
    <span >上传时间</span>
    <input type="text"  name="movie[year]" id="time" >
    //name 和 class 是自己命名的,写什么无所谓,id 等于的 time 只要和下面的名字对应就行
    <script>
        Date.prototype.format = function (fmt) {
            var o = {
		"y+": this.getFullYear, //年
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	}
	document.getElementById('time').value = (new Date()).format('yyyy-MM-dd');
    </script>			
</div>

效果如下:

html 自动获取当前时间填充到input框内 1

更多 建站教程 请访问 https://www.mimiwuqi.com/webqianduan/