我的计算如下:
vCurrDate = new Date();
vDueFromDate = new Date();
vDueToDate = new Date();
vOverDueToDate = new Date();
vCurrDate.setDate(vCurrDate.getDate() - 1);
vDueFromDate.setDate(vCurrDate.getDate() - 30);
vDueToDate.setDate(vCurrDate.getDate());
vOverDueToDate.setDate(vCurrDate.getDate() - 31);2018年3月2日(今天),上述所有计算都运行良好。但是,2018年3月1日,最后三次计算出错了。以下是上述两个日期的计算结果:
-2018年3月2日(今日)
清华2018年3月1日12:03:34 GMT+0530 (印度标准时间)* 01 -2018年3月-2018年3月 2018年1月30日星期二12:03:34 GMT+0530 (印度标准时间)* 30 -2018年1月-2018年 清华2018年3月1日12:03:34 GMT+0530 (印度标准时间)* 01 -2018年3月-2018年3月 2018年1月29日12:03:34 GMT+0530 (印度标准时间)*2018年1月29日
-2018年3月1日(昨日)
2018年2月28日(印度标准时间) 12:12:32 GMT+0530 *2018年2月28日 2018年2月26日12:12:32 GMT+0530 (印度标准时间)*代替291-2018年1月-2018年 2018年3月28日星期三12:12:32 GMT+0530 (印度标准时间)* 28 -2018年2月28日 2018年2月25日太阳12:12:32 GMT+0530 (印度标准时间)*28-2018年1月-2018年
发布于 2018-03-02 09:10:26
Hmmm....Look。从文档,我们现在介绍.setDate(..)下一步:
如果dayValue超出了月份的日期值范围,setDate()将相应地更新date对象。例如,如果为dayValue提供了0,则日期将设置为前一个月的最后一天。
让我们了解一下3月1日你的约会发生了什么。
vCurrDate.setDate(vCurrDate.getDate() - 1);
之后,根据vCurrDate规范,将setDate分配到2月28日。
vDueFromDate.setDate(vCurrDate.getDate() - 30);
若要vDueFromDate,请将日期设置为28-30或从当前日期起的-2天。0表示前几个月的最后一天,so -2是2月26日。
vDueToDate.setDate(vCurrDate.getDate());
如前所述,vCurrDate.getDate()现在是28,所以我们将vDueToDate设为当月28,即3月28日。
vOverDueToDate.setDate(vCurrDate.getDate() - 31);
在本例中,您试图将日期设置为当前月份的-3,因此,正如上面所提到的,这是2月25日。
https://stackoverflow.com/questions/49065447
复制相似问题