简体   繁体   English

moment.js startOf问题

[英]moment.js startOf issue

I am trying to get the start and end of a day (which is a few days from today) using moment.js . 我正在尝试使用moment.js获取一天的开始和结束(从今天开始几天)。 This is the code I have: 这是我的代码:

var today = moment();
var day = today.add(-5, "days");
var startOfDay = day.startOf("day");
var endOfDay = day.endOf("day");

console.log("today " + today.format());
console.log("day " + day.format());
console.log("start " + startOfDay.format());
console.log("end " + endOfDay.format());

And these are the logs: 这些是日志:

I2015-11-10T15:19:02.930Z]today 2015-11-10T15:19:02+00:00
I2015-11-10T15:19:02.931Z]day 2015-11-05T15:19:02+00:00
I2015-11-10T15:19:02.932Z]start 2015-11-05T23:59:59+00:00
I2015-11-10T15:19:02.933Z]end 2015-11-05T23:59:59+00:00

As you can see, the start and end dates are exactly the same. 如您所见, start日期和end日期完全相同。 The end date is as expected, however, the startOf function appears to be doing exactly what the endOf function does. end日期与预期的一样,但是startOf函数似乎正在完全执行endOf函数。

Is there perhaps something I am missing? 也许我缺少什么?

Dates are mutable, and are altered by the method calls. 日期是可变的,并且可以通过方法调用进行更改。 Your two dates are both actually the same date object. 您的两个日期实际上都是同一个日期对象。 That is, day.startOf("day") returns the value of day both times you call it. 也就是说, day.startOf("day")返回day的值。 You can make copies however: 您可以复制:

var startOfDay = moment(day).startOf("day");
var endOfDay = moment(day).endOf("day");

That constructs two new instances. 这将构造两个新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM