js精确显示日期与时间

发布时间:2020-10-31编辑:脚本学堂
本文介绍了js精确显示日期与时间的一个例子,有关js日期与时间计算的方法,有需要的朋友参考下。

例子,js日期与时间精确显示代码。
 

复制代码 代码示例:

<!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=utf-8" />
<title>js精确显示日期与时间--www.jb200.com</title>
<script defer language="javascript" type="text/javascript">
function myfun()
{
 document.write("我是神!");
}
function isLeapYear(year)
{
 if((year %4==0 && year %100!=0) || (year %400==0)) return true;
 else return(false);
}
function checkShow(tmpChar)
{
 if(tmpChar<10)tmpChar='0'+tmpChar;
}

var year = 2001;
var month = 10;
var mday = 30;
var hour = 23;
var minute = 59;
var second = 50;

var pyear = year;
var pmonth=month;if(month<10)pmonth='0'+month;
var pmday=mday;if(mday<10)pmday='0'+pmday;
var phour=hour;if(hour<10)phour='0'+hour;
var pminute=minute;if(minute<10)pminute='0'+minute;
var psecond=second;if(second<10)psecond='0'+second;

function jugFun(month)
{
 switch(month)
 {
  case 1:standerDay=31;break;
  case 2:if(isLeapYear(year) == true)standerDay=29;else standerDay=28;break;
  case 3:standerDay=31;break;
  case 4:standerDay=30;break;
  case 5:standerDay=31;break;
  case 6:standerDay=30;break;
  case 7:standerDay=31;break;
  case 8:standerDay=31;break;
  case 9:standerDay=30;break;
  case 10:standerDay=31;break;
  case 11:standerDay=30;break;
  case 12:standerDay=31;break;
  default :break;
 }
 return standerDay;
}
//standerDay=jugFun(month);
//document.write(standerDay);

function showtime()
{
 document.getElementById("myBody").innerHTML=pyear+'年'+pmonth+'月'+pmday+'日'+' '+phour+'时'+pminute+'分'+psecond+'秒';
 second=second+1;
 if(second<10)psecond='0'+second;
 else if(second<60)psecond=second;
 else
 {
  second=0;psecond="00";
  minute=minute+1;
  if(minute<10)pminute='0'+minute;
  else if(minute<60)pminute=minute;
  else
  {
   minute=0;pminute="00";
   hour=hour+1;
   if(hour<10)phour='0'+hour;
   else if(hour<24)phour=hour;
   else
   {
    hour=0;phour="00";
    mday=mday+1;
    if(mday<10)pmday='0'+mday;
    else if(mday<jugFun(month)+1)pmday=mday;
    else
    {
     mday=0;pmday="01";
     month=month+1;standerDay=jugFun(month);
     if(month<10)pmonth='0'+month;
     else if(month<13)pmonth=month;
     else
     {
      month=0;pmonth="01";
      year=year+1;
     }
    }
   }
  }

 }
 setTimeout(showtime,1000);
}
</script>
</head>

<body id="myBody" onload="showtime()">
</body>
</html>