猿问

JavaScript 时间选择器实时插入

连续我有一个时钟,它显示我登录的时间。我怎样才能让它成为现实,所以它一直在滴答作响并显示实时?请看图(20.03.21 22:25 行)

厘米与时间

<input type="text" class="light1" name="Date" value="[[:Date:]]" /><br/>

<input type="text" class="light1" id="txt" name="Now" readonly="readonly" value="[:NOW:]" />

<span style="position:relative; top:0px; left:0px;"><a href="javascript:;" onclick="$('#AddDataForm input[name=Date]').val($('#AddDataForm input[name=Now]').val());" style="text-decoration:none; position:absolute; right:3px; top:-2px;">&uarr;</a></span>



慕桂英546537
浏览 166回答 2
2回答

德玛西亚99

如果您希望文本输入随着时间每秒更新一次,您可以实现如下所示:<input type="text" id="myTime" /><script>&nbsp; &nbsp; window.onload = function () {&nbsp; &nbsp; &nbsp; &nbsp; setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newDate = new Date();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('myTime').value = newDate.toLocaleString();&nbsp; &nbsp; &nbsp; &nbsp; }, 1000);&nbsp; &nbsp; }</script>

莫回无

取自时钟和日期 javascript的代码const pad = num => ("0" + num).slice(-2);const timedate = () => {&nbsp; const currentTime = new Date(new Date().getTime() + diff);&nbsp; let hours = currentTime.getHours();&nbsp; const minutes = pad(currentTime.getMinutes());&nbsp; const seconds = pad(currentTime.getSeconds());&nbsp; const d = currentTime.getDate();&nbsp; const day = pad(d);&nbsp; const month = pad(currentTime.getMonth() + 1);&nbsp; const yyyy = currentTime.getFullYear();/*&nbsp; let dn = "PM"&nbsp; if (hours <= 12) dn = "AM";&nbsp; if (hours >= 12) hours -= 12;&nbsp; if (hours == 0) hours = 12; */&nbsp; hours = pad(hours);&nbsp; timeOutput.value = "" +&nbsp; &nbsp; yyyy + "/" + month + "/" + day +&nbsp; &nbsp; " " +&nbsp; &nbsp; hours + ":" +&nbsp; &nbsp; minutes + ":" +&nbsp; &nbsp; seconds// + dn;}let timeOutput;let serverTime;let diff;window.addEventListener("load", function() {&nbsp; timeOutput = document.getElementById("timedate");&nbsp; serverTime = new Date("2020/03/21 22:23:24");// change to new Date("[[:Date:]]"); for example&nbsp; diff = new Date().getTime() - serverTime.getTime();&nbsp; setInterval(timedate, 1000);});<input type="text" id="timedate" class="light1" name="Date" value="" /><br/>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答