发布网友 发布时间:2022-04-21 09:34
共4个回答
热心网友 时间:2022-04-21 11:04
效果图:
代码截图:(关键部分用框框标出)
源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<span id="date"></span> <br>
<span id="week"></span>
</div>
<script>
var showDate = document.getElementById('date'); // 获取页面元素
var showWeek = document.getElementById('week'); // 获取页面元素
var time = new Date();
var year = time.getFullYear(); //获取年份
var month = correctDate(time.getMonth() + 1); //获取月份
var day = correctDate(time.getDate()); //获取日期
var date = "今天是" + year + "年" + month + "月" + day + "日";
var week = time.getDay(); // 获取星期几,返回的是0/1/2/3/4/5/6 0代表周日
var weeks = [ "星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var toWeek = weeks[week]; // 获取今日星期
showDate.innerHTML = date; // 将日期显示在页面
showWeek.innerHTML = toWeek; // 将星期显示在页面
/* 这是一个规范数字的函数:
当时间小于10时,在数字前面加上0,统一都是用两个数字表示,
比如3月5日就显示03月05日
*/
function correctDate(date){
if(date<10){
return '0'+date;
}
return date;
}
</script>
</body>
</html>
热心网友 时间:2022-04-21 12:22
<!DOCTYPE html">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js显示时间</title>
</head>
<body>
<div id="divBottom"></div>
<script type="text/javascript">
//获取当天日期
function date()
{
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth()+1;
var day=date.getDate();
var hour="00"+date.getHours();
hour=hour.substr(hour.length-2);
var minute="00"+date.getMinutes();
minute=minute.substr(minute.length-2);
var second="00"+date.getSeconds();
second=second.substr(second.length-2);
var week=date.getDay();
switch(week)
{
case 1:week="星期一 ";break;
case 2:week="星期二 ";break;
case 3:week="星期三 ";break;
case 4:week="星期四 ";break;
case 5:week="星期五 ";break;
case 6:week="星期六 ";break;
case 0:week="星期日 ";break;
default:week="";break;
}
document.getElementById("divBottom").innerHTML=year+"年"+month+"月"+day+"日"+" "+hour+":"+minute+":"+second+" "+week;
}
setInterval("date()",1000);
</script>
</body>
</html>
热心网友 时间:2022-04-21 13:56
在浏览器随便打开个网页,按f12,点击第二个console,输入new Date(),查看输出的对象热心网友 时间:2022-04-21 15:48
沉默了很久,EVA问,“你来是要倾诉什么么?”