Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.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 getMonth在添加天数后返回上一个月_Javascript_Date_Datetime - Fatal编程技术网

Javascript getMonth在添加天数后返回上一个月

Javascript getMonth在添加天数后返回上一个月,javascript,date,datetime,Javascript,Date,Datetime,我在今天的日期中添加天数,并以毫秒为单位获取时间戳 for(i=1;i<=shippingDays;i++){ var result = new Date(); result.setTime( result.getTime() + i * 86400000 ); console.log(result); console.log(result.getMonth()+'-'+result.getDate()+'-'+result.getFu

我在今天的日期中添加天数,并以毫秒为单位获取时间戳

  for(i=1;i<=shippingDays;i++){

      var result = new Date();
      result.setTime( result.getTime() + i * 86400000 );
      console.log(result);
      console.log(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
      newDate = new Date(result.getMonth()+'-'+result.getDate()+'-'+result.getFullYear());
     console.log(newDate);
 }; 
第一个控制台返回2016年4月7日星期四18:34:33 GMT+0500 PKT,但稍后返回结果。getMonth始终返回上一个月的值。因此,第二个控制台总是返回3-7-2016,第三个控制台总是返回2016年3月7日星期一00:00:00 GMT+0500 PKT

我的最终目标是从00:00:00开始计算未来几天的毫秒数。比如今天是2016年6月4日。我想得到未来几天的时间戳。时间戳应从该日期开始计算,即00:00:00

有人能告诉我我做错了什么吗

Data.getMonth中的月份是以零为基础的,因此一月是0,二月是1,等等

因此,如果要使用月份值创建新日期,只需添加一个。

数据中的月份。getMonth是以零为基础的,因此一月是0,二月是1,以此类推


因此,如果您想使用月份值创建新日期,只需添加一个。

getMonth是以零为基础的,因此0=一月、1=二月、3=四月等。因此console.log的输出是正确的

getMonth是以零为基础的,因此0=一月、1=二月、3=四月等。因此console.log的输出是正确的

您需要将1添加到getMonth函数中

例如:

console.log((result.getMonth() + 1)+'-'+result.getDate()+'-'+result.getFullYear());
将返回正确的月份/日期。这是因为getMonth函数返回的值为0-11,1月为0,12月为11


您需要在getMonth函数中添加1

例如:

console.log((result.getMonth() + 1)+'-'+result.getDate()+'-'+result.getFullYear());
将返回正确的月份/日期。这是因为getMonth函数返回的值为0-11,1月为0,12月为11