好久没有写简书了,今天整理一下倒计时的代码,可以学习总结一下
直接上代码,这里写了两种,new Date() 的参数传递的不一样,在文章的最后把new Date() 的参数形式也整理了一下,大家可以批评指正一下.
<script language="JavaScript">
var DifferenceHour = -1
var DifferenceMinute = -1
var DifferenceSecond = -1
var Tday = new Date("Dec var Tday =1, 2017 23:59:59") //**倒计时时间点-注意格式
console.log("Tday",Tday)//
var daysms = 24 * 60 * 60 * 1000//一天的秒数
var hoursms = 60 * 60 * 1000//
var Secondms = 60 * 1000
var microsecond = 1000
function clock()
{
var time = new Date()
console.log("time",time)//
var hour = time.getHours()
console.log("hour",hour)
var minute = time.getMinutes()
console.log("minute",minute)
var second = time.getSeconds()
console.log("second",second)
var timevalue = ""+((hour > 12) ? hour-12:hour)
timevalue +=((minute < 10) ? ":0":":")+minute
timevalue +=((second < 10) ? ":0":":")+second
timevalue +=((hour >12 ) ? " PM":" AM")
// document.formnow.now.value = timevalue
var convertHour = DifferenceHour
var convertMinute = DifferenceMinute
var convertSecond = DifferenceSecond
var Diffms = Tday.getTime() - time.getTime()
console.log("Diffms",Diffms)
DifferenceHour = Math.floor(Diffms / daysms)
Diffms -= DifferenceHour * daysms
DifferenceMinute = Math.floor(Diffms / hoursms)
Diffms -= DifferenceMinute * hoursms
DifferenceSecond = Math.floor(Diffms / Secondms)
Diffms -= DifferenceSecond * Secondms
var dSecs = Math.floor(Diffms / microsecond)
if(convertHour != DifferenceHour) document.formnow.dd.value=DifferenceHour
if(convertMinute != DifferenceMinute) document.formnow.hh.value=DifferenceMinute
if(convertSecond != DifferenceSecond) document.formnow.mm.value=DifferenceSecond
document.formnow.ss.value=dSecs
setTimeout("clock()",1000)
}
</script>
以上是一个倒计时的代码,我感觉比较冗长,页面也不是很美观,可以看一下下面这个,
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<script language="javascript" type="text/javascript">
var interval = 1000;
function ShowCountDown(year,month,day,divname)
{
var now = new Date();
var endDate = new Date(year, month-1, day);
var leftTime=endDate.getTime()-now.getTime();
var leftsecond = parseInt(leftTime/1000);
//var day1=parseInt(leftsecond/(24*60*60*6));
var day1=Math.floor(leftsecond/(60*60*24));
var hour=Math.floor((leftsecond-day1*24*60*60)/3600);
var minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60);
var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);
var cc = document.getElementById(divname);
cc.innerHTML = "脚本之家提示距离"+year+"年"+month+"月"+day+"日还有:"+day1+"天"+hour+"小时"+minute+"分"+second+"秒";
}
window.setInterval(
function(){ShowCountDown(2017,12,31,'divdown1')
;}, interval);
</script>
</head>
<body>
<div id="divdown1"></div>
</body>
</html>
以上代码简洁完整,可以直接复制,直接使用的,只用更改参数就可以了,但是也有一个缺点就是页面显示的时候是延迟调用的,各位可以根据自身情况进行改进.
---------------------------------------------------------分割线------------------------------------------------------------------
new Date() 之参数传递
JavaScript下,new Date([params]),参数传递有以下五种方式:
1、new Date("month dd,yyyy hh:mm:ss");
2、new Date("month dd,yyyy");
3、new Date(yyyy,mth,dd,hh,mm,ss); 注意:这种方式下,必须传递整型;
4、new Date(yyyy,mth,dd);
5、new Date(ms); 注意:ms:是需要创建的时间和 GMT时间1970年1月1日之间相差的毫秒数;当前时间与GMT1970.1.1之间的毫秒数:var mills = new Date().getTime();
//各种变量代表的含义是:
month:用英文 表示月份名称,从January到December ,缩写也行(Jan....Dec);
mth:用整数表示月份,从0(1月)到11(12月)
dd:表示一个 月中的第几天,从1到31
yyyy:四位数表示的年份
hh:小时数,从0(午夜)到23(晚11点)
mm: 分钟数,从0到59的整数
ss:秒数,从0到59的整数
ms:毫秒数,为大于等于0的整数
//举个栗子:
new Date("Jun 2,2017 12:00:00"); //Fri Jun 02 2017 12:00:00 GMT+0800 (中国标准时间)
new Date("Jun 2,2017"); //Fri Jun 02 2017 00:00:00 GMT+0800 (中国标准时间)
new Date(2017,5,2,12,0,0); //Fri Jun 02 2017 12:00:00 GMT+0800 (中国标准时间)
new Date(2017,5,2); //Fri Jun 02 2017 00:00:00 GMT+0800 (中国标准时间)
new Date(1496376000000); //Fri Jun 02 2017 12:00:00 GMT+0800 (中国标准时间)
以上输出的都是2017年6月2号的时间
网友评论