获取当前时间并显示在网页上
2016-12-13 14:39
309 查看
首先在页面中我们直接写一个标签,然后给标签定义一个id,
这里我们用什么标签都可以,我们就用<span></span>演示吧,
代码如下:
上面我们实例化了一个Clock的对象,这里我们就在外部定义一个Clock的类,
获取当前时间并进行时间的格式化,代码如下:
之后我们在页面头部中引入该js就好了,
页面效果图
这里我们用什么标签都可以,我们就用<span></span>演示吧,
代码如下:
<span id="clock" ></span> <script type="text/javascript"> var clock = new Clock(); clock.display(document.getElementById("clock")); </script>
上面我们实例化了一个Clock的对象,这里我们就在外部定义一个Clock的类,
获取当前时间并进行时间的格式化,代码如下:
function Clock() { var date = new Date(); this.year = date.getFullYear(); this.month = date.getMonth() + 1; this.date = date.getDate(); this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()]; this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); this.toString = function() { return "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day; }; this.toSimpleDate = function() { return this.year + "-" + this.month + "-" + this.date; }; this.toDetailDate = function() { return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second; }; this.display = function(ele) { var clock = new Clock(); ele.innerHTML = clock.toString(); window.setTimeout(function() {clock.display(ele);}, 1000); }; }
之后我们在页面头部中引入该js就好了,
<script src="js/Clock.js" type=text/javascript></script>
页面效果图
相关文章推荐
- 网页中通过JavaScript 获取当前系统日期和时间
- 网页显示当前时间
- 分享java程序设计中一个小程序:获取当前系统日期和时间在小程序中显示出来
- 利用JS 在网页上获取并显示当前日期 星期
- WINCE应用层设计经验总结-关于当前时间显示和当前时间获取
- UIDatePicke获取当前时间的年、月、日、时、分、秒, //显示成中文格式的
- 动态获取当前时间显示
- ASP.net在网页上显示当前时间,利用AJAX不刷新网页
- VC++获取系统当前时间并显示
- 网页动态显示当前时间
- js获取当前时间并以时钟显示
- JavaScript代码:在网页中动态显示当前日期和时间
- js获取当前时间显示在页面上
- 如何在网页中动态显示当前的系统日期和时间?
- 表单文本框中文本的获取,还有当前时间的显示
- python学习之获取当前时间的方法(可以任意取时间的任何一个对象显示)
- 网页中显示当前时间日期
- VC如何获取并显示当前系统时间
- js获取当前时间显示在页面上并每秒刷新
- 网页实时显示当前时间