如何将日期字符串从 MM DD YYYY 重新排列为 YYYY MM DD?

How to rearrange date string from MM DD YYYY to YYYY MM DD?

我试过 new Date(03/11/2015) 但没用,但 new Date(2015/3/11) 试过了。如何将“03/11/2015”之类的字符串转换为“2015/3/11”?我尝试将 date-fnsformat 方法一起使用,但 format(new Date(03/15/2015), 'YYYY/MM/DD') returns Invalid time value

为什么 Date 构造函数不接受 MM/DD/YYYY 格式的字符串值?

您可以在此处使用正则表达式替换方法:

var input = '03/11/2015';
var output = input.replace(/(\d+)\/(\d+)\/(\d+)/, "//");
var date = new Date(output);
console.log(date);