世界中の時間を表示します。
worldsDataに場所と日本からの時差を入れます。
<div id="world-time"></div>
const worldsData = [
{
location: "東京",
TimeDifference: 0
},
{
location: "ベルリン",
TimeDifference: -8
},
{
location: "上海",
TimeDifference: -1
},
{
location: "ニューヨーク",
TimeDifference: -14
},
{
location: "ロサンゼルス",
TimeDifference: -16
},
]
const content = document.getElementById('world-time');
worldsData.forEach(function (world) {
const now = new Date(new Date().getTime() + world.TimeDifference * 60 * 60 * 1000);
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
const hour = now.getHours();
const minu = now.getMinutes();
const divElement2 = document.createElement('div');
divElement2.innerHTML = `
<ul>
<li>${world.location}</li>
<li>${`${year}年${month}月${day}日 ${hour}:${minu}`}</li>
</ul>
<br><br>
`
content.appendChild(divElement2)
});
参考