JS tips and tricks

From HPCWIKI
Jump to navigation Jump to search

Display JavaScript datetime in 12 hour AM/PM format[1]

only toLocaleTimeString() may behave differently based on region / location

var time = new Date();
time
Thu May 30 2024 11:56:46 GMT+0900 (한국 표준시)
time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true })
'11:56 AM'
time.toLocaleString('ko-KR', { hour: 'numeric', minute: 'numeric', hour12: true })
'오전 11:56'
time.toLocaleString('ko-KR')
'2024. 5. 30. 오전 11:56:46'

References