手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >js 距离某一时间点时间是多少实现代码
js 距离某一时间点时间是多少实现代码
摘要:复制代码代码如下:正计时#thenceThen{font-size:2em;}functionthenceThen(){vardate1=n...

复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cn">

<head>

<title>正计时</title>

<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

<style type="text/css">

#thenceThen{font-size:2em;}

</style>

<script type="text/javascript" language="javascript">

function thenceThen(){

var date1 = new Date(1381741113671);

var totalSecs=(new Date()-date1)/1000;

var days=Math.floor(totalSecs/3600/24);

var hours=Math.floor((totalSecs-days*24*3600)/3600);

var mins=Math.floor((totalSecs-days*24*3600-hours*3600)/60);

var secs=Math.floor((totalSecs-days*24*3600-hours*3600-mins*60));

if (days != 0 ) {

document.getElementById("thenceThen").innerText="答题时间:"+days+"天"+hours+"小时"+mins+"分钟"+secs+"秒";

}else if (hours == 0 && mins == 0) {

document.getElementById("thenceThen").innerText="答题时间:"+secs+"秒";

}else if (hours == 0 && mins != 0) {

document.getElementById("thenceThen").innerText="答题时间:"+mins+"分钟"+secs+"秒";

}else if (hours != 0) {

document.getElementById("thenceThen").innerText="答题时间:"+hours+"小时"+mins+"分钟"+secs+"秒";

}

}

var clock;

window.onload=function(){

clock=self.setInterval("thenceThen()", 500);

}

</script>

</head>

<body>

<div id="thenceThen"></div>

</body>

</html>

=========================================================================================================

上文中 var date1 = new Date(1381741113671);

参数传递见下方(也可接收后台参数):

参数形式有以下5种:

复制代码 代码如下:

new Date("month dd,yyyy hh:mm:ss");

new Date("month dd,yyyy");

new Date(yyyy,mth,dd,hh,mm,ss);

new Date(yyyy,mth,dd);

new Date(ms);

需要注意最后一种形式,参数表示的是需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数。各种函数的含义如下:

month:用英文表示月份名称,从January到December

mth:用整数表示月份,从(1月)到11(12月)

dd:表示一个月中的第几天,从1到31

yyyy:四位数表示的年份

hh:小时数,从0(午夜)到23(晚11点)

mm:分钟数,从0到59的整数

ss:秒数,从0到59的整数

ms:毫秒数,为大于等于0的整数

如:

复制代码 代码如下:

new Date("January 12,2006 22:19:35");

new Date("January 12,2006");

new Date(2006,0,12,22,19,35);

new Date(2006,0,12);

new Date(1137075575000);

【js 距离某一时间点时间是多少实现代码】相关文章:

Javascript 字符串模板的简单实现

JavaScript中的acos()方法使用详解

document.getElementById的一些细节

js去字符串前后空格的实现方法

Node.js实现JS文件合并小工具

免费空间广告万能消除代码

让插入到 innerHTML 中的 script 跑起来的实现代码

用javascript动态注释掉HTML代码

精确到分钟的js日历控件,日期选择器代码

网页里控制图片大小的相关代码

精品推荐
分类导航