我使用了setMonth(),实现了获取当前月份以前的12个月的月份,测试后没有问题。代码如下:
在31号的时候,使用setMonth()方法会出问题,我看下日期确实是31号。然后我采用了在使用setMonth()方法前,先设置日期的方式,即在endDate.setMonth(endDate.getMonth()-i)这句之前加上endDate.setDate(1),成功解决问题。
在31号的时候,使用setMonth()方法会出问题,我看下日期确实是31号。然后我采用了在使用setMonth()方法前,先设置日期的方式,即在endDate.setMonth(endDate.getMonth()-i)这句之前加上endDate.setDate(1),成功解决问题。