获取时间

 
  
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content= "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document title>
head>
<body>
<div class="week"> div>
<div class="date"> div>
<div class="time"> div>
<div class="duan"> div>
body>
<script>
function Mtime(ele,duan){
//ele 添加年月日时分秒的div元素 duan 添加时间段的div元素
setInterval( function(){
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1; //当前月份
var strDate = date.getDate(); //当天日期
var hour = date.getHours(); //当前小时
var minutes = date.getMinutes(); //当前分钟
var seconds = date.getSeconds(); //当前秒
if (month >= 1 && month <= 9) { //判断月加不加0
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
//判断上午下午
if(hour < 6){ document.write( "凌晨好!")}
else if (hour < 9){duan.innerHTML= "早上好"}
else if (hour < 12){duan.innerHTML= "上午好"}
else if (hour < 14){duan.innerHTML= "中午好"}
else if (hour < 17){duan.innerHTML= "下午好"}
else if (hour < 19){duan.innerHTML= "傍晚好"}
else if (hour < 22){duan.innerHTML= "晚上好"}
else { document.write( "夜里好!")}
//显示年月日,时分秒
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + minutes
+ seperator2 + seconds;
//添加到页面中
ele.innerHTML=currentdate;
}, 1000);
}
function week(ele) {
var str = '星期';
var week = new Date().getDay();
switch (week) {
case 0 :
str += "日";
break;
case 1 :
str += "一";
break;
case 2 :
str += "二";
break;
case 3 :
str += "三";
break;
case 4 :
str += "四";
break;
case 5 :
str += "五";
break;
case 6 :
str += "六";
break;
}
ele.innerHTML=str;
}
Mtime( document.getElementsByClassName( "time")[ 0], document.getElementsByClassName( "duan")[ 0]);
week( document.getElementsByClassName( "week")[ 0]);
script>
html>

你可能感兴趣的:(获取时间)