添加一段时间(moment.js)(Add a duration to a moment (moment.js))

系统教程 行业动态 更新时间:2024-06-14 16:58:30
添加一段时间(moment.js)(Add a duration to a moment (moment.js))

时刻版本:2.0.0

阅读文档后 ,我认为这是直截了当(Chrome控制台):

var timestring1 = "2013-05-09T00:00:00Z"; var timestring2 = "2013-05-09T02:00:00Z"; var startdate = moment(timestring1); var expected_enddate = moment(timestring2); var returned_endate = startdate.add(moment.duration(2, 'hours')); returned_endate == expected_enddate // false returned_endate // Moment {_i: "2013-05-09T00:00:00Z", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array[7]…}

这是一个微不足道的例子,但我甚至不能让它上班。 我觉得我在这里遗漏了一些大东西,但我真的不明白。 即使这样似乎不起作用:

startdate.add(2, 'hours') // Moment {_i: "2013-05-09T00:00:00Z", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array[7]…}

任何帮助将不胜感激。

编辑:我的最终目标是制作一个像我在这里工作的二进制状态图表: http : //bl.ocks.org/phobson/5872894

正如你所看到的,当我在处理这个问题时,我正在使用虚拟x值。

Moment version: 2.0.0

After reading the docs, I thought this would be straight-forward (Chrome console):

var timestring1 = "2013-05-09T00:00:00Z"; var timestring2 = "2013-05-09T02:00:00Z"; var startdate = moment(timestring1); var expected_enddate = moment(timestring2); var returned_endate = startdate.add(moment.duration(2, 'hours')); returned_endate == expected_enddate // false returned_endate // Moment {_i: "2013-05-09T00:00:00Z", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array[7]…}

This is a trivial example, but I can't even get it to work. I feel like I'm missing something big here, but I really don't get it. Even this this doesn't seem to work:

startdate.add(2, 'hours') // Moment {_i: "2013-05-09T00:00:00Z", _f: "YYYY-MM-DDTHH:mm:ss Z", _l: undefined, _isUTC: false, _a: Array[7]…}

Any help would be much appreciated.

Edit: My end goal is to make an binary status chart like the one I'm working on here: http://bl.ocks.org/phobson/5872894

As you can see, I'm currently using dummy x-values while I work through this issue.

最满意答案

我想你错过了.add()文档中的一个关键点

通过添加时间来突变原始时刻。

您似乎正在将其视为返回不可变结果的函数。 容易犯错误。 :)

如果使用返回值,则它与您开始的对象相同。 它只是返回为方便链接的方便。

您可以通过克隆这个时刻解决这个问题, 如这里所述 。

另外,你不能仅仅使用==来测试。 您可以将每个时刻格式化为相同的输出,并将其进行比较,也可以使用.isSame()方法。

您的代码现在是:

var timestring1 = "2013-05-09T00:00:00Z"; var timestring2 = "2013-05-09T02:00:00Z"; var startdate = moment(timestring1); var expected_enddate = moment(timestring2); var returned_endate = moment(startdate).add(2, 'hours'); // see the cloning? returned_endate.isSame(expected_enddate) // true

I think you missed a key point in the documentation for .add()

Mutates the original moment by adding time.

You appear to be treating it as a function that returns the immutable result. Easy mistake to make. :)

If you use the return value, it is the same actual object as the one you started with. It's just returned as a convenience for method chaining.

You can work around this behavior by cloning the moment, as described here.

Also, you cannot just use == to test. You could format each moment to the same output and compare those, or you could just use the .isSame() method.

Your code is now:

var timestring1 = "2013-05-09T00:00:00Z"; var timestring2 = "2013-05-09T02:00:00Z"; var startdate = moment(timestring1); var expected_enddate = moment(timestring2); var returned_endate = moment(startdate).add(2, 'hours'); // see the cloning? returned_endate.isSame(expected_enddate) // true

更多推荐

本文发布于:2023-04-15 03:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/cdbfe0a585fba82849d22e15556513a1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:js   moment   duration   Add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!