Reputation: 317
Is there a method to get the time format from the OS or browser in Angular, because I need to show time in user format?!
I tried to find something, but without any result.
Thanks in advance!
Upvotes: 0
Views: 2227
Reputation: 1127
Typically time format depends on user locale. You can easily get LOCALE_ID and then format date
then you can use js function: toLocaleTimeString()
Easiest way to do it will be with moment.
// From 2.8.1 onward
moment.locale(String);
Upvotes: 1
Reputation: 2401
The best way to format time is using the browser built-in Intl API.
const now = new Date()
console.log(
"American: %s \nLocal: %s \nWith Seconds: %s \nHour cycle: %s",
new Intl.DateTimeFormat("en-US", {timeStyle: "short"}).format(now),
new Intl.DateTimeFormat(undefined, {timeStyle: "short"}).format(now),
new Intl.DateTimeFormat(undefined, {timeStyle: "medium"}).format(now),
new Intl.DateTimeFormat(undefined, {timeStyle: "short"}).resolvedOptions().hourCycle
)
From MDN:
hourCycle
The hour cycle to use. Possible values are "h11", "h12", "h23", or "h24".
Upvotes: 2