Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript dayjs从格式字符串返回无效的日期_Javascript - Fatal编程技术网

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插件的工作”-就在顶部,突出显示的黄色框中。。。