-1

I have tried coding a JS world clock. The world clock itself is posted on stackoverflow.com but I do not know how to get output values from "forEach" into html properly. Would anyone tell me the correct coding or reference sites? Thank you.

Ref. How to get list of all timezones in javascript How to get list of all timezones in javascript

JS code below works but it is just my guesswork.(JS beginner)

setInterval(() => {
  let out = "";
  const city = ['Asia/Taipei', 'America/New_York'];

  city.forEach(adjustTime);

  function adjustTime(item) {
    const today = new Date();
    const localTime = today.toLocaleString("ja-JP", {
      timeZone: `${item}`
    });

    out += item + localTime + "<br>";
    document.getElementById("output").innerHTML = out;

  }
}, 1000);
<p id="output"></p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
takekawa
  • 1
  • 1

1 Answers1

0

Your code works. It can be improved. For example like this

I am using

const city = ['Asia/Taipei', 'America/New_York'];
const adjustTime = item => {
  const today = new Date();
  const localTime = today.toLocaleString("ja-JP", {
    timeZone: `${item}`
  });
  return `${item}: ${localTime}`;
};
const out = document.getElementById("output");

setInterval(() => {
  out.innerHTML = city.map(adjustTime).join("<br>");
}, 1000);
<p id="output"></p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236