弹框显示今天是星期几?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
var now = new Date();
var num = now.getDay();
switch(num){
case 0:alert("星期日");
break;
case 1:alert("星期一");
break;
case 2:alert("星期二");
break;
case 3:alert("星期三");
break;
case 4:alert("星期四");
break;
case 5:alert("星期五");
break;
default:alert("星期六");
}
</script>
</html>

这里面最重要不是switch语法,而是先实例一个对象,即var now = new Date();

这个now对象中有很多属性,从众多属性中取出星期属性的值,基本就完成了一大半了。

var num = now.getDay(); 

注意:这里的括号千万不能忘掉了~

猜你喜欢

转载自www.cnblogs.com/hasher/p/9237575.html
今日推荐