<body> 当前时间:<p id="p1"></p> 高考倒计时:<p id="p2"></p> 限时抢购:<p id="p3"></p> </body>
window.onload=function () {
var p1=document.getElementById("p1"),
p2=document.getElementById("p2");
p3=document.getElementById("p3");
showtime1();
showtime2();
showtime3();
}
function showtime1() {
var nowdate=new Date();//创建Date对象nowdate,以获取当前时间
var year=nowdate.getFullYear(),//获取年份
month=nowdate.getMonth()+1,//获取月份,getMonth()得到的是0-11,需要加1
date=nowdate.getDate(),//获取日份
day=nowdate.getDay(),//获取一周中的某一天,getDay()得到的是0-6
week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
h=nowdate.getHours(),
m=nowdate.getMinutes(),
s=nowdate.getSeconds(),
h=checkTime(h),//函数checkTime用于格式化时,分,秒
m=checkTime(m),
s=checkTime(s);
p1.innerHTML=year+"年"+month+"月"+date+"日"+week[day]+h+":"+m+":"+s;
setTimeout(showtime1, 1000);//反复执行函数本身
}
function checkTime(i) {
if (i<10) {
i="0"+i;
}
return i;
}
function showTime2() {
var nowtime=new Date(),//获取当前时间
endtime=new Date("2017/6/6");//定义结束时间
var lefttime=endtime.getTime()-nowtime.getTime(),//距离结束时间的毫秒数
leftd=Math.floor(lefttime/(1000*60*60*24)),//计算天数
lefth=Math.floor(lefttime/(1000*60*60)%24),//计算小时数
leftm=Math.floor(lefttime/(1000*60)%60),//计算分钟数
lefts=Math.floor(lefttime/1000%60);//计算秒数
p2.innerHTML=leftd+"天"+lefth+":"+leftm+":"+lefts;
setTimeout(showTime2, 1000);
}
function showtime3() {
var nowtime=new Date(),
endtime=new Date("2020/1/1,00:00:00"),//设置结束时间
lefttime=parseInt((endtime.getTime()-nowtime.getTime())/1000),
d=Math.floor(lefttime/(60*60*24)),
h=Math.floor(lefttime/(60*60)%24),
m=Math.floor(lefttime/60%60),
s=Math.floor(lefttime%60);
p3.innerHTML=d+"天"+h+"小时"+m+"分"+s+"秒";
setTimeout(showtime3, 1000);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>倒计时效果</title>
<script type="text/javascript">
function checkTime(i) {
if (i<10) {
i="0"+i;
}
return i;
}
window.onload=function () {
var p1=document.getElementById("p1"),
p2=document.getElementById("p2");
showtime1();
showtime2();
showtime3();
}
function showtime1() {
var nowdate=new Date();
var year=nowdate.getFullYear(),//年份
month=nowdate.getMonth()+1,//月份
date=nowdate.getDate(),//日
week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
day=nowdate.getDay(),//getDay获取0-6
h=nowdate.getHours(),
h=checkTime(h),
m=nowdate.getMinutes(),
m=checkTime(m),
s=nowdate.getSeconds(),
s=checkTime(s);
p1.innerHTML=year+"年"+month+"月"+date+"日"+week[day]+h+":"+m+":"+s;
setTimeout(showtime1, 1000);
}
function showtime2() {
var nowtime=new Date(),
endtime=new Date("2017/6/6");
var lefttime=endtime.getTime()-nowtime.getTime(),
leftd=Math.floor(lefttime/(1000*60*60*24)),
lefth=Math.floor(lefttime/(1000*60*60)%24),
leftm=Math.floor(lefttime/(1000*60)%60),
lefts=Math.floor(lefttime/1000%60);
p2.innerHTML=leftd+"天"+lefth+":"+leftm+":"+lefts;
setTimeout(showtime2, 1000);
}
function showtime3() {
var nowtime=new Date(),
endtime=new Date("2020/1/1,00:00:00"),
lefttime=parseInt((endtime.getTime()-nowtime.getTime())/1000),
d=Math.floor(lefttime/(60*60*24)),
h=Math.floor(lefttime/(60*60)%24),
m=Math.floor(lefttime/60%60),
s=Math.floor(lefttime%60);
p3.innerHTML=d+"天"+h+"小时"+m+"分"+s+"秒";
setTimeout(showtime3, 1000);
}
</script>
</head>
<body>
当前时间:<p id="p1"></p>
高考倒计时:<p id="p2"></p>
限时抢购:<p id="p3"></p>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有