计算HH:MM:SS javascript中两个日期之间的时差 [英] calculate time difference between two date in HH:MM:SS javascript

查看:122
本文介绍了计算HH:MM:SS javascript中两个日期之间的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用javascript创建了一个计时器应用程序。
首先,它需要将当前UTC日期带给初始化计时器,并提供一些参考。这是代码

I have created one timer application in javascript. Firstly it takes the current UTC date to init timer with some reference. here's the code

on_timer: function(e) {
            var self = this;
            if ($(e.target).hasClass("pt_timer_start")) {
                var current_date = this.get_current_UTCDate();
                this.project_timesheet_db.set_current_timer_activity({date: current_date});
                this.start_interval();
                this.initialize_timer();
                this.$el.find(".pt_timer_start,.pt_timer_stop").toggleClass("o_hidden");

现在,一旦计时器启动并且经过一定的时间跨度,计时器就具有上述参考的时间 on_timer:函数(e)函数。

Now, Once timer is started and after some time span timer has some elapsed time with reference to above on_timer: function(e) function.

此函数为

start_interval: function() {
            var timer_activity = this.project_timesheet_db.get_current_timer_activity();
            var self = this;
            this.intervalTimer = setInterval(function(){
                self.$el.find(".pt_duration").each(function() {
                    var el_hour = $(this).find("span.hours");
                    var el_minute = $(this).find("span.minutes");
                    var minute = parseInt(el_minute.text());
                    if(minute >= 60) {
                        el_hour.text(_.str.sprintf("%02d", parseInt(el_hour.text()) + 1));
                        minute = 0;
                    }
                    el_minute.text(_.str.sprintf("%02d", minute));
                    var el_second = $(this).find("span.seconds");
                    var seconds = parseInt(el_second.text()) + 1;
                    if(seconds > 60) {
                        el_minute.text(_.str.sprintf("%02d", parseInt(el_minute.text()) + 1));
                        seconds = 0;
                    }
                    el_second.text(_.str.sprintf("%02d", seconds));
                });
            }, 1000);
        },

现在,考虑 el_hour el_minute el_seconds ,我该如何以HH:MM:SS方式计算初始化时间与当前计时器值之间的时差。

Now, considering el_hour, el_minute, el_seconds How to can i count time difference between init time and current timer value in HH:MM:SS manner.

感谢您提前获得帮助

推荐答案

要将H:M:S转换为秒,您可以使用以下简单函数: / p>

To convert H:M:S to seconds, you can use a simple function like:

// Convert H:M:S to seconds
// Seconds are optional (i.e. n:n is treated as h:s)
function hmsToSeconds(s) {
  var b = s.split(':');
  return b[0]*3600 + b[1]*60 + (+b[2] || 0);
}

然后将秒转换回HMS:

Then to convert seconds back to HMS:

// Convert seconds to hh:mm:ss
// Allow for -ve time values
function secondsToHMS(secs) {
  function z(n){return (n<10?'0':'') + n;}
  var sign = secs < 0? '-':'';
  secs = Math.abs(secs);
  return sign + z(secs/3600 |0) + ':' + z((secs%3600) / 60 |0) + ':' + z(secs%60);
}

var a = '01:43:28';
var b = '12:22:46';

console.log(secondsToHMS(hmsToSeconds(a) - hmsToSeconds(b)));  // -10:39:18
console.log(secondsToHMS(hmsToSeconds(b) - hmsToSeconds(a)));  //  10:39:18

您可能要缩写函数名称,以便说:

You may want to abbreviate the function names to say:

toHMS(toSec(a) - toSec(b));  // -10:39:18

请注意,这并不涵盖时间夏令时边界。为此,您需要完全限定的日期,包括年,月和日。使用这些值创建日期对象,查找差异,转换为秒并使用 secondsToHMS 函数。

Note that this doesn't cover where the time may cross a daylight saving boundary. For that you need fully qualified dates that include the year, month and day. Use the values to create date objects, find the difference, convert to seconds and use the secondsToHMS function.

问题标题提到日期,但是内容似乎只提到小时,分钟和秒的字符串。

The question title mentions dates, however the content only seems to mention strings of hours, minutes and seconds.

如果有Date对象,您可以使用以下方法获得它们之间的差值,以毫秒为单位:

If you have Date objects, you can get the difference between them in milliseconds using:

var diffMilliseconds = date0 - date1;

并转换为秒:

var diffSeconds = diffMilliseconds / 1000;

,并使用上面的 secondsToHMS 函数以HH:MM:SS的形式显示:

and present as HH:MM:SS using the secondsToHMS function above:

secondsToHMS((date0 - date1) / 1000);

例如

var d0 = new Date(2014,10,10,1,43,28);
var d1 = new Date(2014,10,10,12,22,46);

console.log( secondsToHMS((d0 - d1) / 1000));  // -10:39:18

这篇关于计算HH:MM:SS javascript中两个日期之间的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆