let d = new Date();
let opts = {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true,
};
console.log(1, Intl.DateTimeFormat('en-IN', opts).format(d));
console.log(2, Intl.DateTimeFormat('en-US', opts).format(d));
output: 1 09:15:37 am, 2 09:15:37 AM
I need to use Indian locale but am pm as capital. Is there any property to opts to set to get the desired result?
I checked that US lacale default to AM PM. Can we change to am pm?
2
Answers
There is also a formatToParts() method that allows locale-aware formatting of strings produced by this Intl.DateTimeFormat object. Though I am not sure if this is what you want as you said without using string method…
Using the formatToParts() method along with .toUpperCase() in conditional and .join()
Using the formatToParts() method with
.toUpperCase()
as described in MDN with a switch stmt and .join()You could check the a/p character and change to AM/PM part, this will give probably the fastest solution, which is critical with formatting many dates: