A small suggestion.

  • 31
  • 3
  • 3

Due to the time difference around the world, we may encounter the following problems when chatting with foreigners: We just got up, and they are already preparing to go to bed. So I wrote a short piece of code as a simple time difference reference model.


<title>不同国家时间对照</title>

<style>

body {

font-family: Arial, sans-serif;

margin: 20px;

}

table {

width: 100%;

border-collapse: collapse;

margin-top: 20px;

}

th, td {

padding: 10px;

text-align: left;

border-bottom: 1px solid #ddd;

}

th {

background-color: #f2f2f2;

}

</style>

<body>

<h1>不同国家时间对照</h1>

<table id="timeTable">

<thead>

<tr>

<th>国家</th>

<th>时间</th>

</tr>

</thead>

<tbody>

<tr>

<td>中国</td>

<td id="chinaTime"></td>

</tr>

<tr>

<td>美国</td>

<td id="usaTime"></td>

</tr>

<tr>

<td>日本</td>

<td id="japanTime"></td>

</tr>

<tr>

<td>澳大利亚</td>

<td id="australiaTime"></td>

</tr>

<tr>

<td>英国</td>

<td id="ukTime"></td>

</tr>

</tbody>

</table>


<script>

function updateTime() {

const now = new Date();

const chinaTime = new Date(now.getTime() + 8 * 3600 * 1000); // UTC+8

const usaTime = new Date(now.getTime() - 5 * 3600 * 1000); // UTC-5

const japanTime = new Date(now.getTime() + 9 * 3600 * 1000); // UTC+9

const australiaTime = new Date(now.getTime() + 10 * 3600 * 1000); // UTC+10

const ukTime = new Date(now.getTime() + 0 * 3600 * 1000); // UTC+0


document.getElementById('chinaTime').textContent = chinaTime.toLocaleTimeString();

document.getElementById('usaTime').textContent = usaTime.toLocaleTimeString();

document.getElementById('japanTime').textContent = japanTime.toLocaleTimeString();

document.getElementById('australiaTime').textContent = australiaTime.toLocaleTimeString();

document.getElementById('ukTime').textContent = ukTime.toLocaleTimeString();

}


setInterval(updateTime, 1000);

updateTime();

</script>

</body>


S**t code (


I only added a few countries, and it runs like this


This is just an immature model, and I hope the website can remind users what time it is in other countries. Whether to make this thing into a separate page or put it in a corner, as long as it can remind users.

(It doesn't necessarily have to be the code I wrote; using something else is also fine.🙂)


Q: Why are the minutes and seconds the same?

F:

Edited by Chang_Xinfa .

@Etienne If you have any time left, I think this would be a great idea as well.

Heya, this is a good idea. Currently we don't store user timezone, but it just so happens that we will soon because of a new feature coming out shortly. Then, it should be pretty easy to implement your idea.

Figuring out the time based on the country alone isn't enough because some countries include multiple timezones (for example, the US comprises of 5 timezones, and Russia has 8). Also, the code you wrote doesn't take into account daylight savings so it would only work half of the year in a number of regions.

I will try to see if this feature can be worked into the site within the next couple of months 🙂