Skip to content

Force displaying am/pm for times in the en-GB locale? #1629

Answered by icambron
mjb8086 asked this question in Q&A
Discussion options

You must be logged in to vote

Sure:

DateTime
     .now()
     .setLocale("en-GB")
     .toLocaleString({...DateTime.DATETIME_MED, "hourCycle": "h11"}) // => '15 May 2024, 1:40 pm'

The argument to toLocaleString() is ultimately passed to the Intl.DateTimeFormat constructor and thus supports everything that does. DateTime.DATETIME_MED is just a precanned object containing formatting options. So you can just glom on extra options, as in my example.

Note that you have to override it not because of anything about DATETIME_MED but actually because of the en-GB locale, which does specify 24-hour time. In my locale ,en-US, there's no debate and we always use 12-hour time, so the locale works like that by default. The UK seems…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by mjb8086
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants