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 >
 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM