JS tips and tricks: Difference between revisions
Jump to navigation
Jump to search
(Phase 1.2: Fix typo tims → tips (copy content to correct title)) Tags: Blanking Reverted |
Tag: Undo |
||
| Line 1: | Line 1: | ||
== Display JavaScript datetime in 12 hour AM/PM format<ref>https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format</ref> == | |||
<code>only toLocaleTimeString() may behave differently based on region / location</code> | |||
<syntaxhighlight lang="javascript"> | |||
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' | |||
</syntaxhighlight> | |||
== References == | |||
<references /> | |||
Revision as of 14:32, 14 July 2026
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'