[英]Multiple locale support with date-fns
Using date-fns in version 2.0.0-alpha.11
, I trying to get the same behavior as the following momentjs calls: 在
2.0.0-alpha.11
版本中使用date- 2.0.0-alpha.11
,我试图获得与以下moment.js调用相同的行为:
moment().format('LL'); // July 26, 2018
moment().format('LLL'); // July 26, 2018 4:59 PM
What I tried is the following: 我尝试了以下内容:
import format from 'date-fns/esm/format';
const formattedDate = format(new Date(2018, 6, 26, 16, 59, 0), 'LL');
In that case formattedDate
returns the month number 07
. 在这种情况下,
formattedDate
返回月份号07
。
In the source code for locales, there are definitions for short, medium, long and full format . 在语言环境的源代码中,有short,medium,long和full格式的定义 。 Is there no ways to get these definitions with the
format
functions based on the current locale? 是否没有办法使用基于当前语言环境的
format
函数来获取这些定义?
I have looked at the documentation and at the release notes but I cannot find a way. 我看过文档和发行说明,但找不到办法。
After looking deeper at the source code, I found a solution: 在深入研究源代码之后,我找到了一个解决方案:
moment().format('LL'); ==> format(new Date(), 'PP');
moment().format('LLL'); ==> format(new Date(), 'PPp');
The output is not strictly equivalent to momentjs but close enough in my case. 输出并不严格等于momentjs,但就我而言足够接近。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.