数字时钟
要求:页面显示形如2015/05/20 15:36:31这样的时间格式,并且时间一秒走一次。
JavaScript代码:
<script type="text/javascript" src="/Scripts/jquery-1.8.2.min.js"></script> <script type="text/javascript"> function getdate() { var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); $("#sTime").html(year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second); window.setTimeout(getdate, 1000); } $(function () { getdate(); }); </script>
Html代码:
<span id="sTime"></span>