Skip to content

Instantly share code, notes, and snippets.

@gwsu2008
Created May 27, 2021 17:28
Show Gist options
  • Save gwsu2008/e2c64704c45b7f57375610b34bb7c8bd to your computer and use it in GitHub Desktop.
Save gwsu2008/e2c64704c45b7f57375610b34bb7c8bd to your computer and use it in GitHub Desktop.
java-script-time.js
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the date and time as a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
var n = d.toLocaleString("en-US");
var offset = d.getTimezoneOffset();
if (offset == 420) {
timezone = " PDT"
} else if (offset == 480) {
timezone = " PST"
} else {
timezone = ""
}
document.getElementById("demo").innerHTML = n + timezone;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment