如何使用javascript从新日期动态获取最近三个月的开始日期

如何使用javascript从新日期动态获取最近三个月的开始日期,javascript,date,time,momentjs,Javascript,Date,Time,Momentjs,从新日期(包括当前月份的开始日期)动态获取最近三个月的开始日期,并将其推送到数组中。预期输出将为[新日期'09-01-2019',新日期'08-01-2019',新日期'07-01-2019',新日期'06-01-2019']我的日期格式为MM-DD-YYYY在javascript中是否可以这样做?尝试以下操作: 设d=新日期; d、 设定日期1; 让dteList=[new Dated.setMonthd.getMonth,new Dated.setMonthd.getMonth-1,new

从新日期(包括当前月份的开始日期)动态获取最近三个月的开始日期,并将其推送到数组中。预期输出将为[新日期'09-01-2019',新日期'08-01-2019',新日期'07-01-2019',新日期'06-01-2019']我的日期格式为MM-DD-YYYY在javascript中是否可以这样做?

尝试以下操作:

设d=新日期; d、 设定日期1; 让dteList=[new Dated.setMonthd.getMonth,new Dated.setMonthd.getMonth-1,new Dated.setMonthd.getMonth-1,new Dated.setMonthd.getMonth-1] console.logdteList 新日期'09-01-2019'返回日期对象。在某些实现中,它将表示无效日期

如果需要日期对象数组,最好使用值调用日期构造函数,而不是构建字符串并使用内置解析器。还可以使用一个函数,该函数接受返回日期数和开始日期的可选参数,例如

//从开始日期返回n个日期的数组 函数getLastNMonthsn=0,d=new Date{ 设y=d.getFullYear, m=d.getMonth, 日期=[]; 做{ dates.pushnew Datey,m-,1; }n-; 返回日期; } console.log“当前月份:”; getLastNMonths.forEachd=>console.logd.toString; 控制台。记录“当前加上前6个月:”; getLastNMonths6.forEachd=>console.logd.toString; 控制台。日志“2000年1月1日及之前3个月:”;
getLastNMonths3,new Date2000,0,1.forEachd=>console.logd.toString;是的,这是可能的,但是,要实现这一目标,你面临什么问题?你尝试过什么?@CalvinNunes感谢我用这个代码const newDate=new Date09-01-2019尝试过的回复;常量dateArr=[];newDate.setMonthnewDate.getMonth-4;dateArr.pushnewDate.toLocaleDateString;我只得到了第四个月的开始日期,请在问题中添加您的尝试,而不是作为评论,使用“为我”,新日期“09-01-2019”返回无效日期。看见