您的位置:首页 > Web前端 > JavaScript

js获取当前时间与星期几,并自动跟随时间变动

2018-01-21 14:37 267 查看
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style>
.a{background:red;width:350px;height:80px;color:#ffffff;font-size:13px;line-height:80px;text-align:center}
</style>
</head>
<body>
<div class="a" id="x"></div>
<br/>
现在时间:<span id="time"></span>

<script>
setInterval(function(){
var myDate = new Date();
var yue=myDate.getMonth()+1;
var x=myDate.getFullYear()+"年"+yue+"月"+myDate.getDate()+"日 "+myDate.getHours()+"点"+myDate.getMinutes()+"分"+myDate.getSeconds()+"秒";
document.getElementById("x"). innerHTML=x;
},1000);
</script>
<script language="javascript">
function showtime()
{
var today,hour,second,minute,year,month,date;
var strDate ;
today=new Date();
var n_day = today.getDay();
switch (n_day)
{
case 0:{
strDate = "星期日"
}break;
case 1:{
strDate = "星期一"
}break;
case 2:{
strDate ="星期二"
}break;
case 3:{
strDate = "星期三"
}break;
case 4:{
strDate = "星期四"
}break;
case 5:{
strDate = "星期五"
}break;
case 6:{
strDate = "星期六"
}break;
case 7:{
strDate = "星期日"
}break;
}
//year = today.getYear();
year = today.getFullYear();
month = today.getMonth()+1;
date = today.getDate();
hour = today.getHours();
minute =today.getMinutes();
second = today.getSeconds();
//document.getElementById('time').innerHTML = year + "年" + month + "月" + date + "日 " + strDate +" " + hour + ":" + minute + ":" + second; //显示时间
document.getElementById('time').innerHTML = year + "年" + month + "月" + date + "日 " +" " + hour + ":" + minute + ":" + second +" "+strDate; //显示时间
setTimeout("showtime();", 1000); //设定函数自动执行时间为 1000 ms(1 s)
}
</script>
<script language="javascript"> showtime();</script><br/>
</body>
</html>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: