Programming/JavaScript

[JavaScript] ํƒ€์ž„์กด time zone offset ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•

yuri lee 2023. 1. 7. 15:57
๋ฐ˜์‘ํ˜•

Intro

์•ˆ๋…•ํ•˜์„ธ์š”. ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” JavaScript๋กœ ํƒ€์ž„์กด time zone offset์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ์— ์•ž์„œ ์šฐ์„  ํƒ€์ž„์กด ๊ฐœ๋…์„ ์ž ๊น ํ›‘์–ด๋ณด๊ณ  ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

What is a time zone?

ํƒ€์ž„์กด(time zone)์€ ๋™์ผํ•œ ๋กœ์ปฌ ์‹œ๊ฐ„์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋ฉฐ, ํ†ต์ƒ์ ์œผ๋กœ ๊ตญ๊ฐ€๋ณ„ ๊ณ ์œ ํ•œ ํƒ€์ž„์กด์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

 

Offset?

์˜คํ”„์…‹์€ UTC์™€์˜ ์ฐจ์ด๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ์‹œ๋กœ UTC+09:00 ์—์„œ +9:00๋Š” UTC์˜ ๊ธฐ์ค€์‹œ๊ฐ„๋ณด๋‹ค 9์‹œ๊ฐ„์ด ๋น ๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋Œ€ํ•œ๋ฏผ๊ตญ์˜ ํƒ€์ž„์กด์€ KST (Korea Standard Time) ์ด๋ผ๊ณ  ๋ถˆ๋ฆฌ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. 

 

How to get a offset?

์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ offset์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

console.log(new Date().toTimeString().slice(9));
console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log(new Date().getTimezoneOffset() / -60);


// GMT+0900 (ํ•œ๊ตญ ํ‘œ์ค€์‹œ)
// Asia/Seoul
// 9

 


https://stackoverflow.com/questions/1091372/getting-the-clients-time-zone-and-offset-in-javascript

https://ratseno.tistory.com/11

๋ฐ˜์‘ํ˜•