javascript 如何使用 getTime() 从两个日期正确计算 JS 中的剩余时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22130907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:30:44  来源:igfitidea点击:

How to properly calculate remaining time in JS using getTime() from the two dates?

javascriptdatedatetimegoogle-apps-scripthumanize

提问by craftApprentice

I'm trying to calculate remaining time (ex: 10 years, 2 months and 10 days from today(2014/03/02) in JS using this function:

我正在尝试使用此函数在 JS 中计算剩余时间(例如:从今天(2014/03/02)算起的 10 年、2 个月和 10 天:

var d2 = new Date(2024, 3, 12);
var d1 = new Date();
var d0 = new Date(1970, 0, 1);

var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) );
var years = diff.getFullYear();
var months = diff.getMonth();
var days = diff.getDay();

alert("remaining time = " + years + " years, " + months + " months, " + days + " days.");

But instead of get the 10 years difference, I got 1980 years difference (though the days difference I understand that are produced buy the variation of days in months and years):

但是,我得到了 1980 年的差异,而不是 10 年的差异(尽管我理解的天数差异购买了以月和年为单位的天数变化):

enter image description here

在此处输入图片说明

Is it possible to perform this "remaining time" operation using this strategy? If so, how to get the expected result?

是否可以使用此策略执行此“剩余时间”操作?如果是这样,如何得到预期的结果?

Here the function in a JS shell: jsfiddle.net/3ra6c/

这里是 JS shell 中的函数:jsfiddle.net/3ra6c/

采纳答案by Xotic750

As I explained, Dateis not equipped with such functions and you have to do the math yourself. You can get the milliseconds difference between two dates, but then it's down to doing the math to represent that as you wish.

正如我所解释的,Date没有配备这样的功能,你必须自己做数学。您可以获得两个日期之间的毫秒差异,但是接下来就需要根据自己的意愿进行数学运算来表示它了。

An example of using moment.js, loaded with require.js, with humanized approximations.

使用moment.js,加载require.js,具有人性化近似值的示例。

Javascript

Javascript

require.config({
    paths: {
        moment: 'http://momentjs.com/downloads/moment.min'
    }
});

require(['moment'], function (moment) {
    var x = moment(new Date(2024, 3, 12)).from(new Date());

    console.log(x);
});

Output

输出

in 10 years

On jsFiddle

jsFiddle 上

Look at their docs to see how you can humanize the output, you may want a little more detail.

查看他们的文档以了解如何将输出人性化,您可能需要更多细节。

回答by craftApprentice

I find herethe solution I was looking for:

我在这里找到了我正在寻找的解决方案:

var date1 = new Date();
var date2 = new Date(2015, 2, 2);
var diff = new Date(date2.getTime() - date1.getTime());

var years = diff.getUTCFullYear() - 1970; // Gives difference as year
var months = diff.getUTCMonth(); // Gives month count of difference
var days = diff.getUTCDate()-1; // Gives day count of difference

alert("remaining time = " + years + " years, " + months + " months, " + days + " days.");

And it seems to work very well!

它似乎工作得很好!

回答by Martin Diphoorn

var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) )

var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) )

Why do you add d0? Try to remove it.

为什么要加d0?尝试将其删除。