I have to publish an online program for an international conference. Rather than just showing the date and time of each event according to our timezone (UTC+11), I want the page to display them according to the user's timezone. When the page loads it should show what the server thinks the user's timezone is, but give the user the opportunity to override it, using a dropdown.
I have managed to get this to work:
<script>
function convertTZ(date, tzString) {
return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString}));
}
</script>
<body onload = "document.getElementById('datetime').innerHTML = convertTZ('2021/01/09 11:00:00 +1100','Europe/Paris')">
<p id="datetime"></p>
</body>
But how to I get the user’s timezone string and allow the user to override this? I also will need to extract just the date + time without the timezone info (eg "GMT+1100 (Australian Eastern Daylight Time)") but hopefully I can work that out myself.
(I am very much a JavaScript beginner.)
Any help would be greatly appreciated, particularly if someone knows of something that already exists, which I could adapt. Thanks.