我正在使用 date-fns
,我想根据用户选择的区域设置(语言+区域)显示日期,例如:en_US 为“2021 年 1 月 12 日” 或 “2021 年 1 月 12 日”。对于en_CA。,有办法用date-fns
来实现这一点吗?
最佳答案
format
的 P
模式处理本地化:https://date-fns.org/v2.25.0/docs/format
看起来您需要 PP
变体,并且可以像这样使用:
import { caLocale } from 'date-fns/locale/ca'
const result = format(new Date(2021, 0, 12), "PP", {
locale: caLocale
})
关于javascript - 使用 date-fns 根据用户区域设置格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69731895/