當(dāng)前位置:首頁文章首頁 IT學(xué)院 Javascript

最常見的js日歷時(shí)間代碼分享

作者:  來源:  發(fā)布時(shí)間:2011-6-20 15:17:13  點(diǎn)擊:

 本文提供給大家分享的是最常用的js日歷時(shí)間代碼,希望能給大家派上用場。

(推薦)當(dāng)前時(shí)間年月日時(shí)分秒自動(dòng)變化

<font id="itstudy"> </font>
<script>
<!--
setInterval("itstudy.innerHTML=new Date().toLocaleString()+'星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);
//-->
</script>

當(dāng)前時(shí)間年月日時(shí)分秒自動(dòng)變化

<script>
function show(){
var date = new Date(); //日期對(duì)象
var now = "";
now = date.getFullYear()+"年"; //讀英文就行了
now = now + (date.getMonth()+1)+"月"; //取月的時(shí)候取的是當(dāng)前月-1如果想取當(dāng)前月+1就可以了
now = now + date.getDate()+"日";
now = now + date.getHours()+"時(shí)";
now = now + date.getMinutes()+"分";
now = now + date.getSeconds()+"秒";
document.getElementById("nowDiv").innerHTML = now; //div的html是now這個(gè)字符串
setTimeout("show()",1000); //設(shè)置過1000毫秒就是1秒,調(diào)用show方法
}
</script>
<body onload="show()"> <!-- 網(wǎng)頁加載時(shí)調(diào)用一次 以后就自動(dòng)調(diào)用了-->
<div id="nowDiv"></div>
</body>

自動(dòng)計(jì)時(shí)器

<script language="javascript">
<!--
var CallTimeLen = "0";
var timer1 = null;

function DoCallTimer()

  var minute="0";
     var second="0";
  CallTimeLen = parseInt(CallTimeLen)+1;
  minute = parseInt(CallTimeLen/60);
  second = CallTimeLen%60;
  if(minute=="0")
  {
   document.getElementById("thzt").innerHTML ="已經(jīng)過了:"+second+"秒";
  }
  else
  {
   document.getElementById("thzt").innerHTML="已經(jīng)過了:"+minute+"分"+second+"秒";
  }
  window.timer1 = window.setTimeout("DoCallTimer()",1000);
}
function stop()
{
 clearTimeout(window.timer1);
}
-->
</script>

<hr>
<span id="thzt">
<script type="text/javascript">
<!--
 DoCallTimer();
//-->
</script>


</span>

手動(dòng)計(jì)時(shí)器

<script language="javascript">
<!--
var CallTimeLen = "0";
var timer1 = null;

function DoCallTimer()

  var minute="0";
     var second="0";
  CallTimeLen = parseInt(CallTimeLen)+1;
  minute = parseInt(CallTimeLen/60);
  second = CallTimeLen%60;
  if(minute=="0")
  {
 startButton.value="計(jì)時(shí)開始";

   document.getElementById("thzt").innerHTML ="已經(jīng)過了:"+second+"秒";
  }
  else
  {
   document.getElementById("thzt").innerHTML="已經(jīng)過了:"+minute+"分"+second+"秒";
  }
  window.timer1 = window.setTimeout("DoCallTimer()",1000);
}

function stop()
{
 startButton.value="繼續(xù)計(jì)時(shí)";

 clearTimeout(window.timer1);
}

function reset(){
 startButton.value="開始計(jì)時(shí)";
 document.getElementById("thzt").innerHTML="";
 if(timer1!=null){
  time1=null;
  CallTimeLen="0";
 }
}

-->
</script>

<hr>
<span id="thzt"></span>
<br>

<hr>
<input type="button" value="開始計(jì)時(shí)" name="startButton" onClick="DoCallTimer()">
<input type="button" value="暫停" name="stop" onClick="stop();">
<input type="button" value="重置" name="reset" onClick="reset();">

日歷

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript 日歷</title>
<script language="JavaScript">
<!--
function Calendar(name){
 document.write('<div id="calendar"></div>');
 this.name = name;
 this.now = new Date();
 this.year = this.now.getFullYear();
 this.month = this.now.getMonth();
 this.day = this.now.getDate();
 this.monthName = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
 this.weekName = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
 this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
 this.element = document.getElementById('calendar');
 this.init();
}
Calendar.prototype.init = function() {
 var strHtml = '';
 strHtml += this.createHead();
 strHtml += this.createWeek();
 strHtml += this.createDays();
 strHtml += this.createFoot();
 this.element.innerHTML = strHtml;
}
Calendar.prototype.getDays = function() {
 if (1 == this.month) return ((0 == this.year % 4) && (0 != (this.year % 100))) ||(0 == this.year % 400) ? 29 : 28;
 else return this.daysInMonth[this.month];
}
Calendar.prototype.createHead = function() {
 return '<span class="head">' + this.monthName[this.month] + ' ' + this.year + '</span>';
}
Calendar.prototype.createWeek = function() {
 var strResult = '';
 for (var i = 0; i < 7; i++) {
  if (i == 0 || i == 6) strResult += '<span class="we">';
  else strResult += '<span class="ww">';
  strResult += this.weekName[i].substr(0,2) + '</span>';
 }
 return strResult;
}
Calendar.prototype.createDays = function() {
 var strResult = '';
 var i = 0;
 var d = this.getDays();
 var n = Math.ceil(d / 7) * 7;
 var w = new Date(this.year, this.month, 1).getDay();
 for (var j = 0; j < w; j++) { //輸出前空格
  i += 1;
  strResult += '<span>&nbsp;</span>';
 }
 for (var j = 1; j <= d; j++) { //輸出日期格
  i += 1;
  if (j == this.day) {
   strResult += '<span class="today">' + j + '</span>';
  } else {
   var k = new Date(this.year, this.month, j).getDay();
   if (k == 0 || k == 6) {
    strResult += '<span class="weekend">' + j + '</span>';
   } else {
    strResult += '<span>' + j + '</span>';
   }
  }
 }
 for (var j = 0; j < (Math.ceil(i / 7) * 7 - i); j++) { //輸出后空格
  strResult += '<span>&nbsp;</span>';
 }
 return strResult;
}
Calendar.prototype.createFoot = function() {
 return '<span class="foot"><a href="javascript:' + this.name + '.changeMonth(\'p\');" class="arrow">3</a> MONTH <a href="javascript:' + this.name + '.changeMonth(\'n\');" class="arrow">4</a>  <a href="javascript:' + this.name + '.changeYear(\'p\');" class="arrow">3</a> YEAR <a href="javascript:' + this.name + '.changeYear(\'n\');" class="arrow">4</a></span>';
}
Calendar.prototype.changeYear = function(arg) {
 if (arg == 'p') this.year -= 1;
 if (arg == 'n') this.year += 1;
 this.init();
}
Calendar.prototype.changeMonth = function(arg) {
 var m;
 if (arg == 'p') m = this.month - 1;
 if (arg == 'n') m = this.month + 1;
 if (arg == 'p' || arg == 'n') {
  if ( m > -1 && m < 12) {
   this.month = m;
  } else if (m < 0) {
   this.year -= 1;
   this.month = 11;
  } else if (m > 11) {
   this.year += 1;
   this.month = 0;
  }
 }
 this.init();
}
//-->
</script>
<style type="text/css">
#calendar {
 width: 175px;
 border: 1px solid #cccccc;
 border-bottom: none;
 border-left: none;
}
#calendar span {
 width: 24px;
 color: #999999;
 font-size: 9px;
 font-family: Verdana, sans-serif;
 font-weight: normal;
 text-align: center;
 background-color: #FFFFFF;
 border-bottom: 1px solid #cccccc;
 border-left: 1px solid #cccccc;
 padding: 2px 0px 1px 0px;
 margin: 0px;
 float: left;
}
#calendar span.today {
 color: #0066ff;
 font-weight: bold;
 background-color: #f6f6f6;
}
#calendar span.head {
 width: 164px;
 color: #FFFFFF;
 text-align: left;
 font-weight: bold;
 font-family: Tahoma, sans-serif;
 background-color: #0033CC;
 border-bottom: none;
 padding: 2px 5px 1px 5px;
}
#calendar span.foot {
 width: 174px;
 color: #999999;
 text-align: center;
 font-weight: bold;
 font-family: Tahoma, sans-serif;
 background-color: #F0F0F0;
 padding: 0px;
}
#calendar a.arrow {
 color: #666666;
 font-family: webdings;
 font-weight: normal;
 text-decoration: none;
}
#calendar a:hover.arrow {
 color: #0000FF;
}
#calendar .ww, #calendar .we {
 color: #666666;
 font-weight: bold;
 font-family: Tahoma, sans-serif;
 background-color: #F0F0F0;
}
#calendar span.weekend, #calendar .we {
 color: #FF0000;
}
</style>
</head>

<body>
<script language="JavaScript">
<!--
cal = new Calendar('cal');
//-->
</script>
</body>
</html>

相關(guān)軟件

相關(guān)文章

文章評(píng)論

軟件按字母排列: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z