Quantcast
Channel: Getting the client's time zone (and offset) in JavaScript - Stack Overflow
Viewing all articles
Browse latest Browse all 34

Answer by mikakun for Getting the client's time zone (and offset) in JavaScript

$
0
0

as mentioned by others, to get a timezone :

const tz = Intl.DateTimeFormat().resolvedOptions().timeZone

Not mentioned before, to get the offset from the timezone, use locale "ia" (see https://stackoverflow.com/a/64262840/1061871)

const getOffset = (tz) => Intl.DateTimeFormat("ia", {                timeZoneName: "shortOffset",                timeZone : tz              })                .formatToParts()                .find((i) => i.type === "timeZoneName").value // => "GMT+/-hh:mm"                .slice(3); //=> +/-hh:mm console.log(tz +' UTC'+ getOffset(tz))

Viewing all articles
Browse latest Browse all 34

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>