Javascript Clock - 换行符

时间:2016-06-02 02:10:15

标签: javascript

这是我当前使用的代码(来自教程),我想知道是否有办法让时间显示在日期之上?

function renderTime(){
  // Date
  var mydate = new Date();
  var year = mydate.getYear();
    if(year < 1000){
      year +=1900;
    }
  var day = mydate.getDay();
  var month = mydate.getMonth();
  var daym = mydate.getDate();
  var dayarray = new     Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  var montharray = new     Array("January","February","March","April","May","June","July","August","September","October","November","December");
  // Date End

  // Time
  var currentTime = new Date();
  var h = currentTime.getHours();
  var m = currentTime.getMinutes();
  var s = currentTime.getSeconds();
    if( h == 24){
      h = 0;
    }
    else if(h > 12){
      h = h - 0;
    }
// adding 0 infront of single digit values
    if(h < 10){
      h = "0" + h;
    }

    if(m < 10){
      m = "0" + m;
    }

    if(s < 10){
      s = "0" + s;
    }

    var myClock = document.getElementById("clockDisplay");
    myClock.textContent = "" +dayarray[day]+ " " +montharray[month] + " " +daym+ " " +h+ ":" +m+ ":" +s;
    myClock.innerText = "" +dayarray[day]+ " " +montharray[month] + " " +daym+ " " +h+ ":" +m+ ":" +s;

    setTimeout("renderTime()", 1000);
    // Time End
}
renderTime();

我不确定.textContent和.innerText是做什么的 - 我是初学者。 HTML:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <link rel="stylesheet" href="css/style.css" />
    <script type="text/javascript" src="js/scripts.js"></script>
  </head>
  <body onLoad="renderTime()">
    <div id="clockDisplay" class="container"></div>
  </body>
<html>

CSS:

.container {
  font-family: verdana;
  font-weight: normal;
  color: #000000;
  text-align: right;
  font-size: 20px;
}

0 个答案:

没有答案