javascript 计算两次javascript之间的时间差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16188119/
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 03:37:46  来源:igfitidea点击:

Calculate time difference between two times javascript

javascriptdatetimegettime

提问by pistoleta

i've looking around how to do this and i found a lot of examples with complicated code. Im using this:

我环顾四周如何做到这一点,我发现了很多复杂代码的例子。我使用这个:

var time1 = new Date();
var time1ms= time1.getTime(time1); //i get the time in ms  

then i do this in other part of the code

然后我在代码的其他部分执行此操作

var time2 = new Date();
var time2ms= time2.getTime(time2); 

and finnally:

最后:

var difference= time2ms-time1ms;
var lapse=new Date(difference);  
label.text(lapse.getHours()+':'+lapse.getMinutes()+':'+lapse.getSeconds());

This works great, except for one issue, the hours it gaves me are always +1 so i have to add to the code (time.getHours()-1)otherwise it gaves me one hour more....

这很好用,除了一个问题,它给我的时间总是 +1,所以我必须添加到代码中 (time.getHours()-1)否则它给了我一个小时......

I think is an easier way to do it than all the other examples around... but i still dont understand why i need to add '-1' in order to have the correct lapse.

我认为这是一种比周围所有其他示例更简单的方法……但我仍然不明白为什么我需要添加“-1”才能正确失效。

THanks!!!

谢谢!!!

回答by MaxArt

The problem is your timezone.

问题是你的时区。

When you do new Date(difference), you're creating a Dateobject that represent the moment exatcly differencemilliseconds after January 1st, 1970. When you do lapse.getHours()your timezone is used in the computation. You cannot modify your timezone via Javascript, and cannot modify this behaviour. Not without some heavyJavascript tricks.

当您这样做时new Date(difference),您正在创建一个Date对象,该对象代表difference1970 年 1 月 1 日之后精确毫秒的时刻。当您这样做时,lapse.getHours()您的时区将用于计算。您不能通过 Javascript 修改您的时区,也不能修改此行为。并非没有一些沉重的Javascript 技巧。

But your differencedoes notrepresent a date, but a differenceof dates. Treat is as such, and compute the hours, minutes and seconds like this:

但是,你difference不能代表日期,但差别日期。对待就是这样,并像这样计算小时、分钟和秒:

var hours = Math.floor(difference / 36e5),
    minutes = Math.floor(difference % 36e5 / 60000),
    seconds = Math.floor(difference % 60000 / 1000);

Alternatively, you can take your timezone into account when creating lapse:

或者,您可以在创建时考虑您的时区lapse

var lapse = new Date(difference + new Date().getTimezoneOffset() * 1000);

but I wouldn't recommend this: Dateobjects are overkill for your purposes.

但我不建议这样做:Date对象对于您的目的来说太过分了。

回答by Matthijs

Note that in the getTimezoneOffset()returns a value in minutes, so if you want to use the lapse, you can correct it for the timezone difference like this:

请注意,在getTimezoneOffset()返回一个以分钟为单位的值,因此如果您想使用延时,您可以像这样针对时区差异更正它:

lapse = new Date(difference); 
tz_correction_minutes = new Date().getTimezoneOffset() - lapse.getTimezoneOffset();
lapse.setMinutes(offset_date.getMinutes() + tz_correction_minutes);

now you can do:

现在你可以这样做:

label.text(lapse.getDate()-1+' days and'  +lapse.getHours()+':'+lapse.getMinutes()+':'+lapse.getSeconds());

to print out the time difference in human readable form

以人类可读的形式打印时差