js 距離某一時間點時間是多少實現(xiàn)代碼
更新時間:2013年10月14日 17:46:12 作者:
距離某一時間點時間是多少,在本文將為大家介紹下js中是如何實現(xiàn)的,感興趣的朋友不要錯過
復(fù)制代碼 代碼如下:
<!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);
參數(shù)傳遞見下方(也可接收后臺參數(shù)):
參數(shù)形式有以下5種:
復(fù)制代碼 代碼如下:
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);
需要注意最后一種形式,參數(shù)表示的是需要創(chuàng)建的時間和GMT時間1970年1月1日之間相差的毫秒數(shù)。各種函數(shù)的含義如下:
month:用英文表示月份名稱,從January到December
mth:用整數(shù)表示月份,從(1月)到11(12月)
dd:表示一個月中的第幾天,從1到31
yyyy:四位數(shù)表示的年份
hh:小時數(shù),從0(午夜)到23(晚11點)
mm:分鐘數(shù),從0到59的整數(shù)
ss:秒數(shù),從0到59的整數(shù)
ms:毫秒數(shù),為大于等于0的整數(shù)
如:
復(fù)制代碼 代碼如下:
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);
相關(guān)文章
理解Javascript_02_理解undefined和null
其實在 ECMAScript 的原始類型中,是有Undefined 和 Null 類型的。 這兩種類型都分別對應(yīng)了屬于自己的唯一專用值,即undefined 和 null。2010-10-10js實現(xiàn)鼠標(biāo)滾輪控制圖片縮放效果的方法
這篇文章主要介紹了js實現(xiàn)鼠標(biāo)滾輪控制圖片縮放效果的方法,涉及onmousewheel事件及javascript操作圖片的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-02-02Layui數(shù)據(jù)表格跳轉(zhuǎn)到指定頁的實現(xiàn)方法
今天小編就為大家分享一篇Layui數(shù)據(jù)表格跳轉(zhuǎn)到指定頁的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09