JS復(fù)習(xí)作業(yè):Date等函數(shù)題,倒計(jì)時(shí),鐘表,自定義毫秒轉(zhuǎn)化為時(shí)間【詩書畫唱】

案例:通過Date函數(shù)寫一個(gè)得到當(dāng)前時(shí)間的函數(shù),格式為“2222年02月11日?11:09:01?星期3”

var dangQian=new Date();
var year=dangQian. getFullYear();
var month=dangQian. getMonth()+1;
var day=dangQian. getDate();
var zhouji=dangQian. getDay();
var xiaoshi=dangQian . getHours();
var fenzhong=dangQian . getMinutes();
var miao=dangQian. getSeconds();
var divJieMianVar=document . getElementById("divJieMian");
divJieMianVar. innerHTML="當(dāng)前時(shí)間為"+year+"年"+month+"月"+day+"日"+xiaoshi+":"+fenzhong+":"+miao;


得到當(dāng)前時(shí)間,打印當(dāng)前時(shí)間的毫秒數(shù)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
window. onload=function(){
var dangQian=new Date();
var year=dangQian. getFullYear();
var month=dangQian. getMonth()+1;
var day=dangQian. getDate();
var zhouji=dangQian. getDay();
var xiaoshi=dangQian . getHours();
var fenzhong=dangQian . getMinutes();
var miao=dangQian. getSeconds();
var divJieMianVar=document . getElementById("divJieMian");
var zongMiaoShuCha=?
? ? parseInt(dangQian.getTime()) ;
divJieMianVar. innerHTML="當(dāng)前時(shí)間為"+year+"年"
+month+"月"+day+"日"+xiaoshi+":"+fenzhong+":"+miao+";當(dāng)前時(shí)間轉(zhuǎn)換毫秒數(shù)為"+zongMiaoShuCha+"毫秒";
}
</script>
</head>
<body>
<div id="divJieMian"></div>
</body>
</html>

或
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
window. onload=function(){
var dangQian=new Date();
var year=dangQian. getFullYear();
var month=dangQian. getMonth()+1;
var day=dangQian. getDate();
var zhouji=dangQian. getDay();
var xiaoshi=dangQian . getHours();
var fenzhong=dangQian . getMinutes();
var miao=dangQian. getSeconds();
var divJieMianVar=document . getElementById("divJieMian");
var zongMiaoShuCha=?
? ? parseInt(dangQian.valueOf()) ;
divJieMianVar. innerHTML="當(dāng)前時(shí)間為"+year+"年"
+month+"月"+day+"日"+xiaoshi+":"+fenzhong+":"+miao
+";當(dāng)前時(shí)間轉(zhuǎn)換毫秒數(shù)為"+zongMiaoShuCha+"毫秒";
}
</script>
</head>
<body>
<div id="divJieMian"></div>
</body>
</html>


聲明一個(gè)變量10000000,將該毫秒傳入到時(shí)間中,打印該時(shí)間

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
window. onload=function(){
var s=10000000
var dangQian=new Date(s);
var year=dangQian. getFullYear();
var month=dangQian. getMonth()+1;
var day=dangQian. getDate();
var zhouji=dangQian. getDay();
var xiaoshi=dangQian . getHours();
var fenzhong=dangQian . getMinutes();
var miao=dangQian. getSeconds();
var divJieMianVar=document . getElementById("divJieMian");
divJieMianVar. innerHTML="當(dāng)前時(shí)間為"+year+"年"
+month+"月"+day+"日"+xiaoshi+":"+fenzhong+":"+miao;
}
</script>
</head>
<body>
<div id="divJieMian"></div>
</body>
</html>


案例:將上個(gè)例子改寫為一個(gè)時(shí)鐘

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
window. onload=function(){
setInterval(shiZhongFunction,1000);}
function shiZhongFunction(){
var dangQian=new Date();
var year=dangQian. getFullYear();
var month=dangQian. getMonth()+1;
var day=dangQian. getDate();
var zhouji=dangQian. getDay();
var xiaoshi=dangQian . getHours();
var fenzhong=dangQian . getMinutes();
var miao=dangQian. getSeconds();
var divJieMianVar=document . getElementById("divJieMian");
divJieMianVar. innerHTML="當(dāng)前時(shí)間為"+year+"年"
+month+"月"+day+"日"+xiaoshi+":"+fenzhong+":"+miao;
}
</script>
</head>
<body>
<div id="divJieMian"></div>
</body>
</html>

案例:寫一個(gè)倒計(jì)時(shí)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
var i;
function daoJishiFunction(){
if(i==0){
alert("倒計(jì)時(shí)結(jié)束,"+"倒計(jì)時(shí)還有"+i+"秒");
?zanTing();
}
var div1=document . getElementById("div1");
div1. innerHTML="倒計(jì)時(shí)還有"+i+"秒";
i--;
}
var jieshou=null;
function start(){
if(jieshou==null){
jieshou=setInterval(daoJishiFunction, 1000);
}}
function zanTing(){
clearInterval(jieshou);
jieshou=null;
}
window . onload=function(){
i=prompt("請(qǐng)輸入倒計(jì)時(shí)的時(shí)間");
}
</script>
</head>
<body>
<div id="div1"></div>
<input type= "button" id= "btn1" value="開始倒計(jì)時(shí)"onclick= "start()" />
<input type= "button" id= "btn2" value="暫 停" onclick="zanTing()" />
</body>
</html>



這里顯示為1,但實(shí)際上已經(jīng)為0秒:

