一、Date日期对象
1、Date对象:日期对象用于处理日期和时间
2、获得当日的日期
3、常用方法:
getFullYear():获取年份
getTime():获取毫秒
setFullYear():设置具体的日期
getDay():获取星期
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> var date = new Date(); // document.write(date); // document.write(date.getFullYear()); // document.write(date.getTime()); date.setFullYear(2012,1,1); document.write(date); </script> </body> </html>
4、时钟实例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body > <script> function startTime(){ var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); m = checkTime(m); s = checkTime(s); document.getElementById("timetxt").innerHTML = h+":"+m+":"+s; t = setTimeout(function(){ startTime(); },500); } function checkTime(i){ if(i<10){ i = "0" + i; } return i; } </script> <div id="timetxt"></div> </body> </html>
时间: 2024-10-22 06:26:49