Created
August 18, 2021 15:59
-
-
Save acip/c947c123b325d91a53693432acc20615 to your computer and use it in GitHub Desktop.
Display UTC time in local time with JavaScript without dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Local time</title> | |
</head> | |
<body> | |
Local time for 2011-10-10T14:48:00Z is: <time datetime="2011-10-10T14:48:00Z" data-format="hh:mm:ss A"></time> | |
<script> | |
const times = document.getElementsByTagName("time") | |
for (let t of times) { | |
const dt = t.getAttribute("datetime") | |
const date = new Date(dt) | |
t.innerText = date.toLocaleString() | |
// TODO use the data-format attribute to format the date | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment