时间戳的制作![在这里插入图片描述]()


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>时间戳的制作</title>
<style>
h2{
width: 400px;
margin: 100px auto;
text-align: center;
}
</style>
</head>
<body>
<pre>
作业:封装一个函数,能正常输出日期和时间,如2017年12月13日星期三15:04:04
求出,今天距离XXXXX发生的时期过去了多少天,多少小时,分钟
1937年12月13日 10:01:00
</pre>
<h2 id="res"></h2>
<script>
var res= document.getElementById("res");
var stringDate="";
var upToDate="";
var times=getDate();
stringDate =times.year+"年"+times.month+"月"+times.date+"日\t"+times.weeks+"\t "+times.hours+":"+times.miutes+":"+times.seconds;
console.log(stringDate);
var toMinu=(times.miutes-1);
setInterval(function(){
upToDate="今天距XXXXX发生已有:<br>"+"<b style='color:red'>"+(((times.year-1938)*365)+18+monthDay(times.month,times.date,times.year))+"</b>"+"天"+"<b style='color:red'>"+(times.hours-10)+"</b>"+"小时"+"<b style='color:red'>"+toMinu+"</b>"+"分钟";
res.innerHTML=upToDate;
},600);
function monthDay(month,d,year){
var tian=0;
switch(month*1){
case 12:tian+=30;
case 11:tian+=31;
case 10:tian+=30;
case 9:tian+=31;
case 8:tian+=31;
case 7:tian+=30;
case 6:tian+=31;
case 5:tian+=30;
case 4:tian+=31;
case 3:
if(year%4===0&&year%100!==0||year/400===0)
{
tian+=29;
}else{
tian+=28;
}
case 2:tian+=31;
case 1:tian+=d;
return tian;
}
}
function getDate(){
var date=new Date();
var y=date.getFullYear();
var m=date.getMonth();
var myDate=date.getDate();
var week=date.getDay();
switch(week){
case 0:week="星期天";break;
case 1:week="星期一";break;
case 2:week="星期二";break;
case 3:week="星期三";break;
case 4:week="星期四";break;
case 5:week="星期五";break;
case 6:week="星期六";break;
}
var hours=date.getHours();
var minus=date.getMinutes();
var secon=date.getSeconds();
var minu=date.getMilliseconds();
return {
year:addZero(y),
month:addZero(m),
date:addZero(myDate),
weeks:addZero(week),
hours: addZero(hours),
miutes:addZero(minus),
seconds:addZero(secon),
};
}
function addZero(a){
return a<10? "0"+a:a;
}
</script>
</body>
</html>