html天数计数器代码和html显示时间代码

2018-3-6 亿零 Html/Css/Js

html天数计数器代码

<script language="javascript">
var now=new Date();var spday=new Date(2007,04-1,09);a=(now.getTime()-spday.getTime())/(24*60*60*1000);a=Math.ceil(a); document.write
("<b>"+a+"</b>");</script>天 其中2007,04-1,09  是你要計算的天數起始日~(2007,04-1,09)=(西元年,月-1,日),
(-1)的地方不要更動

html显示时间代码

Clock.js文件
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);
 };
}

需要显示时间的页面
<SCRIPT src="js/Clock.js" type=text/javascript></SCRIPT>      //引入Clock.js文件,注意路径
 
<label id=clock></label>       //放入标签,插入时间
<SCRIPT type=text/javascript>      //实例化clock对象
var clock = new Clock();
clock.display(document.getElementById("clock"));
</SCRIPT>

 

标签: JS html

评论:

免费网上赚钱
2018-06-21 21:57
很实用的代码,谢谢
陈双义博客
2018-05-15 10:22
[大拇指]

发表评论:

Powered by emlog 陕ICP备16016230号