JS获取当前时间并把时间赋给input表单

<script>
window.onload = function(){
function getDate(){
debugger;
var today = new Date();
var date;
date = (today.getFullYear()) +"-" + (today.getMonth() + 1 ) + "-" + today.getDate() + "-" + today.toLocaleTimeString();
return date;
}
window.setInterval(function(){
document.getElementById("getBookTime").value=getDate();
}, 1000);
}
</script>

....

<input type="text"  name="getBookTime" id="getBookTime" value="">

另外如何自定义日期格式如下:

选择日期:<input type="date" value="2011-01-04" /> 选择时间:<input type="time" value="22:52" /> 选择星期:<input type="week" /> 选择月份:<input type="month" />
结果如下:
选择日期:

选择时间:

选择星期:

选择月份:


另一种写法如下:
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=gb2312" />
< title >系统时间</ title >
< script language = "javascript" type = "text/javascript" >
<!--
     //获得当前时间,刻度为一千分一秒
     var initializationTime = (new Date()).getTime();
     function showLeftTime() {
         var now = new Date();
         var year = now.getYear();
         var month = now.getMonth();
         var day = now.getDate();
         var hours = now.getHours();
         var minutes = now.getMinutes();
         var seconds = now.getSeconds();
         document.all.show.innerHTML = "2" + year + "年" + month + "月" + day + "日 " + hours + ":" + minutes + ":" + seconds + "";
         //一秒刷新一次显示时间
         var timeID = setTimeout(showLeftTime, 1000);
     }
//-->
</ script >
</ head >
< body onload = "showLeftTime()" >
< label id = "show" >显示时间的位置</ label >
< inpu type = "text" value = "这里是要获取时间的!" />
</ body >
</ html >
 
 
或者如下:
< html >
< head >
< title >无标题文档</ title >
< script >
function t(){
var now= new Date();
var h=now.getHours();
var m=now.getMinutes();
var s=now.getSeconds(); 
if (h>=0 && h <=12)
var ho=" AM";
if (h>12 && h< 24 )
var  ho = " PM" ;
var  tt = h +":"+m+":"+s+ho;
document.getElementById("time") .value = tt ;
}
setInterval('t()',500);
</script>
</ head >
< body >
< form >
< input  type = "text"  id = "time"  value = "" />
</ form >
 
</ body >
</ html >
 

 

 

转载于:https://www.cnblogs.com/zhangliang88/p/5426203.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值