简体   繁体   中英

How can I create a live clock with day of the week, month, day, year and time?

I'm trying to get to work a live clock using JavaScript where it shows the time/date in this format:

Fri Oct 23 2020 13:27:10 GMT-0500 (Central Daylight Time)

I currently have this code where it shows the time 13:27:10 GMT-0500 (Central Daylight Time) but I can't figure out how to show the day of the week, month, day, and year at the beginning.

function startTime() {
  var today = new Date();
  var h = today.getHours();
  var m = today.getMinutes();
  var s = today.getSeconds();
  m = checkTime(m);
  s = checkTime(s);
  document.getElementById('txt').innerHTML =
  h + ":" + m + ":" + s + " GMT-0500 (Central Daylight Time)";
  var t = setTimeout(startTime, 500);
}

function checkTime(i) {
  if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
  return i;
}

I would appreciate any help. Thanks.

The target string format you provided is a result of the toString() method on a standard Date object.

 function startTime() { var today = new Date(); document.getElementById('txt').innerHTML = today.toString(); var t = setTimeout(startTime, 500); } startTime();
 <span id="txt"></span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM