Created
January 1, 2022 08:33
-
-
Save wuriyanto48/9da722cf45630bdcc6948b06f1960cf5 to your computer and use it in GitHub Desktop.
Javascript Timezone
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
// get local timezone offset and convert to millisecond | |
var d = new Date(); | |
var offset = d.getTimezoneOffset(); | |
const localOffsetMillis = 60 * 1000 * offset; | |
console.log(localOffsetMillis); | |
// get local timezone name | |
var dtf = Intl.DateTimeFormat(); | |
var dtfResolveOptions = dtf.resolvedOptions(); | |
var tz = dtfResolveOptions.timeZone; | |
console.log(tz); | |
var nowOnUTC = new Date(); | |
console.log(nowOnUTC); | |
// convert UTC date to local date by adding with localOffsetMillis | |
var localDate = new Date((nowOnUTC.getTime() + Math.abs(localOffsetMillis))); | |
console.log(localDate); | |
var localDateWTZ = nowOnUTC.toLocaleString('ID', {timeZone: tz}); | |
console.log(localDateWTZ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment