λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
κ°œλ°œμƒν™œ/Client

[JS] λΈŒλΌμš°μ € νƒ€μž„μ‘΄ -> utc λ³€ν™˜

by cocococo331 2023. 11. 30.

ν…μŠ€νŠΈλ₯Ό μž…λ ₯ λ°›μ•„ ν˜„μž¬ λΈŒλΌμš°μ € νƒ€μž„μ‘΄μ— 맞게 μ‹œκ°„μ„ λ³€ν™˜ν•œλ‹€.

λΈŒλΌμš°μ € νƒ€μž„μ‘΄μ— λ§žλŠ” ν˜„μž¬ μ‹œκ°„μ„ UTC둜 λ³€ν™˜ν•œλ‹€.

 

// μž…λ ₯된 λ¬Έμžμ—΄ (예: '2023-11-01 00:00:00')
const inputString = '2023-11-01 00:00:00';

// μž…λ ₯된 λ¬Έμžμ—΄μ„ JavaScript의 Date 객체둜 λ³€ν™˜
const inputDate = new Date(inputString);

// λΈŒλΌμš°μ €μ˜ νƒ€μž„μ‘΄μ— 맞게 ν˜„μž¬ μ‹œκ°„μœΌλ‘œ ν‘œμ‹œ
const userTimeZoneDate = inputDate.toLocaleString('en-US', { timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone });

// λΈŒλΌμš°μ €μ˜ νƒ€μž„μ‘΄μ— λ§žλŠ” ν˜„μž¬ μ‹œκ°„μ„ UTC둜 λ³€ν™˜
const utcDate = new Date(userTimeZoneDate).toISOString();

console.log('μž…λ ₯된 λ¬Έμžμ—΄:', inputString);
console.log('μž…λ ₯된 λ¬Έμžμ—΄ Date λ³€ν™˜:', inputDate);
console.log('λΈŒλΌμš°μ € νƒ€μž„μ‘΄μ—μ„œ ν˜„μž¬ μ‹œκ°„:', userTimeZoneDate);
console.log('UTC둜 λ³€ν™˜λœ μ‹œκ°„:', utcDate);