View Code
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>date demo</title> <script type="text/javascript"> function getCurrentTimeString() { var currentDate = new Date(); var year = currentDate.getFullYear(); var month = currentDate.getMonth() + 1; var date = currentDate.getDate(); var hour = currentDate.getHours(); var minutes = currentDate.getMinutes(); var seconds = currentDate.getSeconds(); var timeString = year + '-' + (month.toString().length==2?month:("0"+month))+ '-' + (date.toString ().length==2?date:("0"+date))+ ' ' + (hour.toString().length==2?hour:("0"+hour))+ ':' + (minutes.toString ().length==2?minutes:("0"+minutes))+ ':' + (seconds.toString().length==2?seconds:("0"+seconds)); return timeString; } function refreshTime() { document.getElementById('time').innerHTML = getCurrentTimeString(); //元素的innerHTML属性可以读取和设置元素内 的HTML文本内容 } window.onload = function() { setInterval(refreshTime,1000); } </script> </head> <body> <div id="time"></div> </body> </html>
效果: