有没有一种方法可以从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
使用的小时周期。可能的值为“ h11”,“ h12”,“ h23”或“ h24”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句