【问题标题】:Multiple locale support with date-fns使用 date-fns 支持多语言环境
【发布时间】:2019-01-03 15:34:08
【问题描述】:

2.0.0-alpha.11 版本中使用date-fns,我试图获得与以下momentjs 调用相同的行为:

moment().format('LL');   // July 26, 2018
moment().format('LLL');  // July 26, 2018 4:59 PM

我尝试的是以下内容:

import format from 'date-fns/esm/format';
const formattedDate = format(new Date(2018, 6, 26, 16, 59, 0), 'LL');

在这种情况下,formattedDate 返回月份编号07

在语言环境的源代码中,有definitions for short, medium, long and full format。有没有办法根据当前语言环境使用format 函数获取这些定义?

我查看了documentationrelease notes,但找不到方法。

【问题讨论】:

  • 看看this question,也许会有帮助。
  • 谢谢,我已经尝试过了,但仍然使用 LL 语法获取月份编号,即使作为选项传递了区域设置。

标签: javascript momentjs date-fns


【解决方案1】:

深入查看源码后,找到了解决办法:

moment().format('LL');  ==> format(new Date(), 'PP');
moment().format('LLL'); ==> format(new Date(), 'PPp');

输出并不严格等同于 momentjs 但在我的情况下足够接近。

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 2021-09-17
    • 2012-08-30
    • 2023-01-05
    • 1970-01-01
    • 2019-02-03
    • 2020-03-23
    • 1970-01-01
    • 2013-01-10
    相关资源
    最近更新 更多