在矩JS中执行减法

发布于 2025-02-10 03:11:21 字数 417 浏览 3 评论 0原文

我想从结束值中减去持续时间值。我该如何瞬间做到这一点?我目前正在获取错误无效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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

污味仙女 2025-02-17 03:11:21

您可以使用.add.subtract方法来添加/减去持续时间。

不是您的问题,但我希望持续时间将是开始的,而不是开始端。

这是它的工作方式:

let start = 1977
let end = 1985
let duration = moment(end.toString()).unix() - moment(start.toString()).unix();
let newvalue = moment(end.toString()).subtract(duration, "second");

console.log(newvalue.toString()); // Full date string 
console.log(newvalue.year()); // 1977
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

请注意

我们想劝阻在未来的新项目中使用的时刻

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:

let start = 1977
let end = 1985
let duration = moment(end.toString()).unix() - moment(start.toString()).unix();
let newvalue = moment(end.toString()).subtract(duration, "second");

console.log(newvalue.toString()); // Full date string 
console.log(newvalue.year()); // 1977
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

Take note of the message from the authors of momentjs:

we would like to discourage Moment from being used in new projects going forward

一梦等七年七年为一梦 2025-02-17 03:11:21
  1. 您需要从大价值中减去小价值。
    在您的情况下,您应该从结尾减去启动
  2. 您在UNIX中获得持续时间值,因此您无需再进行一次。
let start = 1977
let end = 1985
let duration = moment(end.toString()).unix() - moment(start.toString()).unix();
let newvalue = moment(end.toString()).unix() - duration;
console.log(duration);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

  1. You need to subtract the small value from the large one.
    Here in your case, you should subtract start from the end
  2. You get the duration value in unix, so you don't need to do it again

let start = 1977
let end = 1985
let duration = moment(end.toString()).unix() - moment(start.toString()).unix();
let newvalue = moment(end.toString()).unix() - duration;
console.log(duration);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

耶耶耶 2025-02-17 03:11:21

这就是我使用Moment.Duration()diff()进行操作的方式。

您可以使用asdays()()在几天内或使用ashours()在数小时内获得持续时间。

请在下面检查摘要:

let start = 1977
let end = 1985
let duration = moment.duration(moment(end.toString()).diff(moment(start.toString())));
console.log(duration.asDays())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

This is how I would do it using moment.duration() and diff().

You can get the duration in days using asDays() or in hours using asHours().

Please check snippet below :

let start = 1977
let end = 1985
let duration = moment.duration(moment(end.toString()).diff(moment(start.toString())));
console.log(duration.asDays())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

习ぎ惯性依靠 2025-02-17 03:11:21

持续时间应该已经在几秒钟内表示持续时间,因此您可以从moment(end.toString()).unix()()(代表自UNIX Epoch):

let newvalue = moment(end.toString()).unix() - duration;

请记住,在代码中,您是从start> start值中减去end值,这是负面的,因为start&lt;结束。我不认为这是打算的,因此您可以使用Math.abs()或从开始中减去 end 。

duration should already represent a duration in seconds, so you can just subtract that from moment(end.toString()).unix() (which represents the number of seconds since the Unix epoch):

let newvalue = moment(end.toString()).unix() - duration;

Keep in mind that in your code you are subtracting the end value from the start value, which is negative since start < end. I don't think this was intended, so you could either use Math.abs() or subtract end from start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文