Skip to content

Instantly share code, notes, and snippets.

@acip
Created August 18, 2021 15:59
Show Gist options
  • Save acip/c947c123b325d91a53693432acc20615 to your computer and use it in GitHub Desktop.
Save acip/c947c123b325d91a53693432acc20615 to your computer and use it in GitHub Desktop.
Display UTC time in local time with JavaScript without dependencies.
<!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