JS tips and tricks: Difference between revisions
Jump to navigation
Jump to search
(Created page with " == 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> == <syntaxhighlight lang="javascript"> toLocaleTimeString() may behave differently based on region / location var time = new Date(); console.log( time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }) ); </syntaxhighlight><code>toLocaleTimeString() may behave differently...") |
m (Admin moved page JS tims and tricks to JS tips and tricks) |
||
(One intermediate revision by the same user not shown) | |||
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> == | == 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"> | <syntaxhighlight lang="javascript"> | ||
var time = new Date(); | 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 == | ||
<references /> | <references /> |
Latest revision as of 12:06, 30 May 2024
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'