After opening the web page, according to the current time, it will display: morning, afternoon, or good evening, and the current system time will be displayed.
0-12am
12:00 to 18:00
Other times are at night, accurate to the second, automatically refreshed every second
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!-- 打开网页后 ,根据当前时间,显示:上午 ,下午,或晚上好,并显示当前系统时间 -->
<script type="text/javascript">
// 定义一个函数
function timer(){
// 获取当前时间
var time_now=new Date();
var year=time_now.getYear()+1900; //年
var month=time_now.getMonth(); //月份
var day=time_now.getDate(); //日
var hour=time_now.getHours(); //小时
var Min=time_now.getMinutes(); //分钟
var s=time_now.getSeconds(); //秒
if(s<10){ //这个是说 秒如果小于10 前面加个0
s="0"+s;
}
var welcome="!欢迎来到惠购商城";
// 判断时间:0-12点上午 12点到18下午 其他时间是晚上
if(hour>=0&&hour<12){
welcome="上午好"+welcome;
}else if(hour>=12&&hour<=18){
welcome="下午好"+welcome;
}else{
welcome="晚上好"+welcome;
}
// 输出
var print="今天日期:"+year+"年"+month+"月"+day+"日<br>现在时间:"+hour+"点"+Min+"分"+s+"秒<br>"+welcome;
document.getElementById("time").innerHTML=print; //将print赋值给它
}
// 定时器 1000毫秒刷新 1000毫秒=1秒
setInterval("timer()",1000);
</script>
</head>
<body>
<!-- 定义一个div id属性 如何定位到它 -->
<div id="time"></div>
</body>
</html>