新的Date();与getHours()不同;

时间:2018-07-29 19:13:00

标签: javascript

你好,我目前正在为我的iPhone开发HTML时间部件,并且理论上已经完成了。但是输出中的时间不正确,而不是系统时间。

这是我当前的所有代码:(我知道css是不需要的,但我知道为什么不这样做。)

非常奇怪的是,当我输出currentTime的内容时,它说的是正确的时间,但是currentTime.getXXXX()是错误的(XXXX是特定的输出,例如getDay)。但是,最奇怪的是时间是正确的,但日期却不是?

function init() {
  updateWidget();
  setInterval("updateWidget();", 1000);
}

function updateWidget() {
  var currentTime = new Date();
  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentWeekdayNUM = currentTime.getDay();
  var currnetDate = currentTime.getDate();
  var currentMonth = currentTime.getMonth();

  if (currentWeekdayNUM = 0) {
    var currentWeekdaySTR = "Sunday";
  }
  if (currentWeekdayNUM = 1) {
    var currentWeekdaySTR = "Monday";
  }
  if (currentWeekdayNUM = 2) {
    var currentWeekdaySTR = "Tuesday";
  }
  if (currentWeekdayNUM = 3) {
    var currentWeekdaySTR = "Wednesday";
  }
  if (currentWeekdayNUM = 4) {
    var currentWeekdaySTR = "Thursday";
  }
  if (currentWeekdayNUM = 5) {
    var currentWeekdaySTR = "Friday";
  }
  if (currentWeekdayNUM = 6) {
    var currentWeekdaySTR = "Saturday";
  }

  if (currentMonth = 0) {
    var currentMonthSTR = "January";
  }
  if (currentMonth = 1) {
    var currentMonthSTR = "February";
  }
  if (currentMonth = 2) {
    var currentMonthSTR = "March";
  }
  if (currentMonth = 3) {
    var currentMonthSTR = "April";
  }
  if (currentMonth = 4) {
    var currentMonthSTR = "May";
  }
  if (currentMonth = 5) {
    var currentMonthSTR = "June";
  }
  if (currentMonth = 6) {
    var currentMonthSTR = "July";
  }
  if (currentMonth = 7) {
    var currentMonthSTR = "August";
  }
  if (currentMonth = 8) {
    var currentMonthSTR = "September";
  }
  if (currentMonth = 9) {
    var currentMonthSTR = "October";
  }
  if (currentMonth = 10) {
    var currentMonthSTR = "November";
  }
  if (currentMonth = 11) {
    var currentMonthSTR = "December";
  }


  if (currentHours <= 9) {
    var currentHours = "0" + currentHours;
  }
  if (currentMinutes <= 9) {
    var currentMinutesSTR = "0" + currentMinutes;
  }

  document.getElementById("hour").innerHTML = currentHours;
  document.getElementById("min").innerHTML = currentMinutesSTR;
  document.getElementById("weekday").innerHTML = currentWeekdaySTR;
  document.getElementById("day").innerHTML = currnetDate;
  document.getElementById("month").innerHTML = currentMonthSTR;
}
body {
    font-family: 'Noto Sans';
    text-align: center;

}
#time {
    font-size:80px;
    margin-bottom: -30px;
}
#date {
    font-size: 20px;
}
<!Doctype HTML>
<html>

<head>
  <title>Pixel Lock</title>

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <script src="code.js"></script>
  <link rel="stylesheet" href="style.css">
  <link href="fonts.css" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
</head>

<body onload="init()">
  <h1 id="time"><span id="hour"></span>:<span id="min"></span></h1>
  <h4 id="date"><span id="weekday"></span>, <span id="month"></span> <span id="day"></span></h4>
</body>

</html>

0 个答案:

没有答案