在React项目中,我得到xxth Mar XXXX
格式的日期输出,我需要DD/MM/YY
格式。
举个例子:2021年3月18日是我得到的,预期产量应该像18/03/2021
什么是最好的解决方案?
您可以使用Moments.js库,然后可以使用moment(date(.format("DD/MM/YYYY"(以获得愿望输出。
我尝试过moment
库,但它不能按预期工作。
const moment = require('moment');
console.log(moment('12th Mar 2021').format('DD/MM/YYYY'));
我收到了以下警告信息,日期又回到了Date()
:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
所以我已经切换到dayjs
,它可以
const customParseFormat = require('dayjs/plugin/customParseFormat');
// without customParseFormat, it won't work
const dayjs = require('dayjs');
dayjs.extend(customParseFormat);
console.log(dayjs('12th Mar 2021', 'DD[th] MMM YY').format('DD/MM/YYYY'));
此外,dayjs
比moment
小得多。