简体   繁体   中英

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:

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 .

In the source code for locales, there are definitions for short, medium, long and full format . Is there no ways to get these definitions with the format functions based on the current locale?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM