距离某一时间点时间是多少,在本文将为大家介绍下 js 中是如何实现的,感兴趣的朋友不要错过
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
- <!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 种: 需要注意最后一种形式,参数表示的是需要创建的时间和 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 的整数 如:
来源: