获取浏览器时间格式 Angular
Get browser time format Angular
有没有办法从 OS 或 Angular 中的浏览器获取时间格式,因为我需要以用户格式显示时间?!
我试图找到一些东西,但没有任何结果。
提前致谢!
设置时间格式的最佳方式是使用浏览器内置 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
)
来自MDN:
hourCycle
The hour cycle to use. Possible values are "h11", "h12", "h23", or "h24".
通常时间格式取决于用户区域设置。您可以轻松获得 LOCALE_ID 然后 format date
然后就可以使用js函数了:toLocaleTimeString()
最简单的方法就是用 moment。
// From 2.8.1 onward
moment.locale(String);
有没有办法从 OS 或 Angular 中的浏览器获取时间格式,因为我需要以用户格式显示时间?!
我试图找到一些东西,但没有任何结果。
提前致谢!
设置时间格式的最佳方式是使用浏览器内置 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
)
来自MDN:
hourCycle
The hour cycle to use. Possible values are "h11", "h12", "h23", or "h24".
通常时间格式取决于用户区域设置。您可以轻松获得 LOCALE_ID 然后 format date
然后就可以使用js函数了:toLocaleTimeString()
最简单的方法就是用 moment。
// From 2.8.1 onward
moment.locale(String);