这里有新鲜出炉的 Javascript 教程,程序狗速度看过来!
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
这篇文章主要为大家详细介绍了 js 实现倒计时效果及时间对象, 具有一定的参考价值,感兴趣的小伙伴们可以参考一下
JS 实现倒计时效果代码如下:
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>
- 无标题文档
- </title>
- h
- <style>
- #box { width: 100%; height: 400px; background: black; color: #fff; font-size:40px;
- line-height:400px; text-align:center; }
- </style>
- <script>
- window.onload = function() {
- var oBox = document.getElementById('box');
- var oDate = new Date(); //获取当前时间;
- oDate.setFullYear(2016, 11, 31); //自动进位;
- oDate.setHours(0, 0, 0, 0);
- function countDown() {
- //未来时间戳减去现在时间的时间戳;
- var ms = oDate.getTime() - new Date().getTime();
- //毫秒转换成秒
- var oSec = parseInt(ms / 1000);
- //秒转换成天
- var oDay = parseInt(oSec / 86400);
- //不到一天剩下的秒数;
- oSec %= 86400;
- //转换成小时
- var oHour = parseInt(oSec / 3600);
- //不到一小时剩下的秒数;
- oSec %= 3600;
- //转换成分钟
- var oMin = parseInt(oSec / 60);
- //不到一分钟剩下的秒数;
- oSec %= 60;
- oBox.innerHTML = '距离2016年12月31日还有:' + oDay + '天' + oHour + '时' + oMin + '分' + oSec + '秒';
- }
- countDown();
- setInterval(countDown, 1000);
- }
- </script>
- </head>
- <body>
- <div id="box">
- 距离2016年12月31日还有:xx天xx时xx分xx秒
- </div>
- </body>
- </html>
实现效果入下:
时间戳:1970 年 1 月日至今的毫秒数: oDate.getTime(); // 不要问我为什么是 1970 年 1 月至今哦!自个儿百度啦!
时间对象:
获取时间:
- var oDate = new Date();
- oYear = oDate.getFullYear();
- oMon = oDate.getMonth();
- oDay = oDate.getDate();
- oHou = oDate.getHours();
- oMin = oDate.getMinutes();
- oSec = oDate.getSeconds();
- oWeek = oDate.getDay();
设置时间:
- oDate.setFullYear(年, 月, 日);
- oDate.setMonth(月);
- oDate.setDate(日);
- oDate.setHours(时, 分, 秒, 毫秒);时间会自动进位;
大概整理的就这些,还有很多不足的地方,希望大家多提宝贵意见!互相学习!互相取经!
来源: http://www.phperz.com/article/17/0710/330467.html