Date 对象
Date 对象用于处理日期和时间。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body{ background: #272822;} .date{ font-family: helvetica,arial,sans-serif; font-size: 70px; color: #ccc; margin: 40px auto; width: 700px; text-align: center; } </style> </head> <body> <div class="date"> </div> <script> // Date 对象 // Date 对象用于处理日期和时间。 // 创建一个日期对象 var currDate = new Date(); // 获取当前年份 currDate.getFullYear(); // 获取当前月份(月份需要加1) currDate.getMonth()+1; // 获取当前天数 currDate.getDate(); // 获取当前星期数(0代表星期日,1代表星期一,以此类推) currDate.getDay(); // 获取当前小时数 currDate.getHours(); // 获取当前分钟数 currDate.getMinutes(); // 获取当前秒数 currDate.getSeconds(); // 将日期对象转换为字符串 currDate.toString(); //时间补零 function toDou(n){ if (n<10) { return '0'+n; }else{ return ''+n; } } var dateHtml = document.getElementsByTagName('div')[0]; setInterval(function(){ var date = new Date(); var day = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'] var currDate = date.getFullYear()+'年'+(date.getMonth()+1)+'月'+date.getDate()+'日'+'<br/>'+toDou(date.getHours())+':'+toDou(date.getMinutes())+':'+toDou(date.getSeconds())+ day[date.getDay()]; dateHtml.innerHTML = currDate; },1000); </script> </body> </html>