在矩JS中执行减法
我想从结束值中减去持续时间值。我该如何瞬间做到这一点?我目前正在获取错误无效ISO-8601日期格式
。
let start = 1977
let end = 1985
let duration = moment(start.toString()).unix() - moment(end.toString()).unix();
let newvalue = = moment(end.toString()).unix() - moment(duration.toString()).unix();
我对持续时间有效的计算,所以我认为将其复制为 newValue ,但这是行不通的。我错过了什么吗?它必须处于ISO-8601格式。
I want to subtract the duration value from the end value. How can I do that with moment.js? I am currently getting the error not valid ISO-8601 date format
.
let start = 1977
let end = 1985
let duration = moment(start.toString()).unix() - moment(end.toString()).unix();
let newvalue = = moment(end.toString()).unix() - moment(duration.toString()).unix();
The calculation I have for duration works, so I thought to replicate it for newvalue, but that doesn't work. Am I missing anything? It must be in ISO-8601 format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
.add
或.subtract
方法来添加/减去持续时间。不是您的问题,但我希望持续时间将是开始的,而不是开始端。
这是它的工作方式:
请注意
You can use
.add
or.subtract
methods to add/subtract a duration.Not your question, but I would expect the duration to be end-start, not start-end.
Here is how it could work:
Take note of the message from the authors of momentjs:
在您的情况下,您应该
从结尾减去启动
Here in your case, you should
subtract start from the end
这就是我使用
Moment.Duration()
和diff()
进行操作的方式。您可以使用
asdays()()
在几天内或使用ashours()
在数小时内获得持续时间。请在下面检查摘要:
This is how I would do it using
moment.duration()
anddiff()
.You can get the duration in days using
asDays()
or in hours usingasHours()
.Please check snippet below :
持续时间
应该已经在几秒钟内表示持续时间,因此您可以从moment(end.toString()).unix()()
(代表自UNIX Epoch):请记住,在代码中,您是从
start> start
值中减去end
值,这是负面的,因为start<结束
。我不认为这是打算的,因此您可以使用Math.abs()
或从开始
中减去 end 。duration
should already represent a duration in seconds, so you can just subtract that frommoment(end.toString()).unix()
(which represents the number of seconds since the Unix epoch):Keep in mind that in your code you are subtracting the
end
value from thestart
value, which is negative sincestart < end
. I don't think this was intended, so you could either useMath.abs()
or subtractend
fromstart
.