能够获取/设置时刻实例到本月的最后一天会很棒 - 类似于 eod。
我想现在这有效:
moment().add('months', 1).date(1).subtract('days', 1)
此外,如果它们超出范围,setter 就会溢出。
moment().date(0); // the last day of last month
moment().date(-1); // the second to last day of last month
moment().hours(25); // 1 am the next day
所以你可以做这样的事情......
moment().add('months', 1).date(0);
moment().endOf('month');
从 1.7 开始工作
moment().endOf('month');
或者
moment(year-month-day).endOf('month');
@lance-ding 谢谢! 那绝对是有帮助的。
我仍然认为为当月的天数设置一个.format()
选项会很好。
相关:您还可以使用moment().daysInMonth()
来获取表示该月最后一天的整数。
有关的:
弃用警告:
moment().add(period, number) is deprecated. Please use moment().add(number, period).
所以对代码稍作改动: moment().add(1, 'months').date(1).subtract('days', 1)
@gideonapollob ,是的。 您还需要在.subtract()
反转。
但是.endOf()
几乎可以肯定是您正在做的事情的最佳选择。
你可能会得到一个月的最后一天。 像这样。
moment(2017-10).endOf('month').format('DD')
显示上个月的最后日期
moment().subtract(1, 'months').endOf('month').format('DD-MM-YYYY')
最有用的评论
从 1.7 开始工作