Javascript dayjs从格式字符串返回无效的日期
我试图从字符串中解析日期,但得到的日期无效 似乎我在这里做了一些不正确的事情,但不知道为什么。我给出了要解析的字符串和格式。正如我所说,我得到了无效的日期 如何正确地做Javascript dayjs从格式字符串返回无效的日期,javascript,Javascript,我试图从字符串中解析日期,但得到的日期无效 似乎我在这里做了一些不正确的事情,但不知道为什么。我给出了要解析的字符串和格式。正如我所说,我得到了无效的日期 如何正确地做 import dayjs from 'dayjs'; console.clear(); const i = '28-04-2021'; const b = dayjs(i, 'DD-MM-YYYY').toDate(); console.log({ b }); // output invalid date. 根据您的情
import dayjs from 'dayjs';
console.clear();
const i = '28-04-2021';
const b = dayjs(i, 'DD-MM-YYYY').toDate();
console.log({ b }); // output invalid date.
根据您的情况,您缺少这行代码
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);
然后您可以使用这种类型的构造函数dayjs(日期,格式)
为了在全球范围内使用它,我建议将它放在项目的顶部。在你的例子中,把它放在导入
dayjs
之后,我知道我可以使用dayjs插件。但是,即使没有插件,我也有一个构造函数,它需要很多重载。在其中一个选项中,我有ctor(string,format)
。所以我问,因为dayjs似乎没有插件。这让我很困惑:“这取决于CustomParseFormat插件的工作”-就在顶部,突出显示的黄色框中。。。