1.简单的显示年月日、星期、时间
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>当前系统时间</title> <link rel="stylesheet" href="style.css" /> <script language="javascript" type="text/javascript"> window.onload = function(){ showTime(); } function checkTime(i){ //补位处理 var i; if(i<10){ i="0"+i; } return i; } function showTime(){ var now=new Date(); var year=now.getFullYear() ; var month=now.getMonth()+1; var day=now.getDate(); var h=now.getHours(); var m=now.getMinutes(); var s=now.getSeconds(); m=checkTime(m); s=checkTime(s); var d=now.getDay(); var weekday=new Array(7) weekday[0]="星期日" weekday[1]="星期一" weekday[2]="星期二" weekday[3]="星期三" weekday[4]="星期四" weekday[5]="星期五" weekday[6]="星期六" document.getElementById("show").innerHTML=""+year+"年"+month+"月"+day+"日 "+weekday[d]+h+":"+m+":"+s; t=setTimeout(‘showTime()‘,500); } </script> </head> <body> <div class="content1"> <div id="show">显示时间的位置</div> </div> </body> </html>
2.制作倒计时
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>研究生考试时间</title> <link rel="stylesheet" href="style.css" /> <script type="text/javascript"> window.onload = function(){ var timedate= new Date("2017,12,25"); //自定义结束时间 var now = new Date() ; //获取当前时间 var date = timedate.getTime() - now.getTime(); //得出的为毫秒 var time = Math.ceil(date/(1000*60*60*24)) ; //1000 * 60 * 60 * 24一天的秒数 if(time > 0 ){ document.getElementById(‘timeShow‘).innerHTML = time; }else{ alert("时间已经到了"); } } </script> </head> <body> <div class="content2"> <div class="txtshow">距离设置时间还有<span id="timeShow"></span>天</div> </div> </body> </html>
时间: 2024-11-10 08:25:48