CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2025-04-22
by mikakun

Original Post

Original - Posted on 2022-12-05
by mikakun



            
Present in both answers; Present only in the new answer; Present only in the old answer;


use locale "ia" with `Intl.DateTimeFormat` :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->


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('Asia/Dubai', ' UTC' + getOffset('Asia/Dubai'))
// to get your user timezone :
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone
console.log(tz + ' UTC' + getOffset(tz))
<!-- end snippet -->
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)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->

const tz = Intl.DateTimeFormat().resolvedOptions().timeZone
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))

<!-- end snippet -->

        
Present in both answers; Present only in the new answer; Present only in the old answer;