Home  >  Article  >  Web Front-end  >  JS method to dynamically load the current time_javascript skills

JS method to dynamically load the current time_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:15:041102browse

The example in this article describes the method of dynamically loading the current time in JS. Share it with everyone for your reference. The specific implementation method is as follows:

<body bgcolor="#fef4d9" onload ="time()">
  <script language="JavaScript">
function time ()
  {
  var now = new Date();
  var yr = now.getYear();
  var mName = now.getMonth() + 1;
  var dName = now.getDay() + 1;
  var dayNr = ((now.getDate()<10) &#63; "0" : "")+ now.getDate();
  var ampm = (now.getHours() >= 12) &#63; " PM" : " AM"
  var hours = now.getHours();
     hours = ((hours > 12) &#63; hours - 12 : hours);
  var minutes = ((now.getMinutes() < 10) &#63; ":0" : ":") + now.getMinutes();
  var seconds = ((now.getSeconds() < 10) &#63; ":0" : ":") + now.getSeconds();
  if(dName==1) Day = "星期日";
  if(dName==2) Day = "星期一";
  if(dName==3) Day = "星期二";
  if(dName==4) Day = "星期三";
  if(dName==5) Day = "星期四";
  if(dName==6) Day = "星期五";
  if(dName==7) Day = "星期六";
  if(mName==1) Month="1月";
  if(mName==2) Month="2月";
  if(mName==3) Month="3月";
  if(mName==4) Month="4月";
  if(mName==5) Month="5月";
  if(mName==6) Month="6月";
  if(mName==7) Month="7月";
  if(mName==8) Month="8月";
  if(mName==9) Month="9月";
  if(mName==10) Month="10月";
  if(mName==11) Month="11月";
  if(mName==12) Month="12月";
  var DayDateTime=(yr+ "年"+" "+ Month+ dayNr+ "日"+ Day+ " "
    + hours+ minutes+ seconds+ " "+ ampm
    );
  document.getElementById ("face").innerText=DayDateTime ;
  timerID = setTimeout("time()",1000);
  }

</script>

<form id ="form">
  <div>
   <asp:Label ID ="face" runat ="server" ></asp:Label>
  </div>
</form>
</body>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn