Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.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 MomentJs:endOf(“月”)未在月的最后一天交付_Javascript_Momentjs - Fatal编程技术网

Javascript MomentJs:endOf(“月”)未在月的最后一天交付

Javascript MomentJs:endOf(“月”)未在月的最后一天交付,javascript,momentjs,Javascript,Momentjs,我的初始日期的console.log显示: Fri Apr 01 2016 02:00:00 GMT+0200 (CEST) 我就是这么做的: // first day is guaranteed to be a first day of month by default var firstDayOfMonth = moment.utc(initialDate).startOf('day'); var lastDayOfMonth = firstDayOfMonth.clone()

我的初始日期的console.log显示:

Fri Apr 01 2016 02:00:00 GMT+0200 (CEST)
我就是这么做的:

  // first day is guaranteed to be a first day of month by default
  var firstDayOfMonth = moment.utc(initialDate).startOf('day');
  var lastDayOfMonth = firstDayOfMonth.clone();
  lastDayOfMonth.endOf('month');
  console.log(firstDayOfMonth.toDate()); // Fri Apr 01 2016 02:00:00 GMT+0200 (CEST)
  console.log(lastDayOfMonth.toDate()); // Sun May 01 2016 01:59:59 GMT+0200 (CEST)
但4月的最后一天当然不是5月1日

这可能与夏季的CEST有关?但是我需要它在任何时区工作-我已经尝试将任何东西设置为UTC以消除这些恼人的时区问题。

使用矩.format而不是toDate来获得UTC。有关不同的格式,请参阅此处的多语言环境支持

比如说,

console.log(lastDayOfMonth.format('LLLL')); 

output: Saturday, April 30, 2016 11:59 PM

日期以当地时间显示,而力矩对象为UTC。请参见