【问题标题】:MomentJs: endOf('month') does not deliver last day of monthMomentJs:endOf('month') 不交付月份的最后一天
【发布时间】:2017-02-01 13:15:31
【问题描述】:

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 以摆脱这些烦人的时区问题。

【问题讨论】:

标签: javascript momentjs


【解决方案1】:

使用moment.format() 而不是toDate() 来获取UTC。对于不同的格式,请参阅Multiple Locale Support 这里https://momentjs.com/

例如,

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多