如何计算在不同日期中以24小时制表示的两个时间之间的差异?

huangapple go评论114阅读模式
英文:

How can I calculate the difference between two times that are in 24 hour format which are in different dates?

问题

在JavaScript中,如何计算24小时制下具有不同日期的两个时间之间的差异?

示例:
Date1是2019/12/31上午11:00:06
Date2是2020/01/01下午01:10:07。
时间差应该以hh:MM:ss格式为02:10:13。
在应用脚本中,当日期发生变化时,如何获得这样的结果?

英文:

In JavaScript, how can I calculate the difference between two times that are in 24 hour format which are having different dates?

Example:
Date1 is 2019/12/31 11:00:06 AM
Date2 is 2020/01/01 01:10:07 PM.
Time difference should be 02:10:13 in hh:MM:ss format
..how can get like this when date changes in appscript

答案1

得分: 1

只需使用 Date

const dateDiffMs = (date1, date2) => {
    const d1 = new Date(date1);
    const d2 = new Date(date2);
    return d1.getTime() - d2.getTime();
}

const ms2hms = (ms) => {
    const sec = Math.floor(ms / 1000);
    const min = Math.floor(sec / 60);
    const h = Math.floor(min / 60);

    return [
        h,
        min % 60,
        sec % 60,
    ];
};

const format = (n) => n < 10 ? '0' + n : n;

const hms2str = ([h, min, sec]) => {
    return `${h}:${format(min)}:${format(sec)}`
}

alert(hms2str(ms2hms(dateDiffMs('2020/01/01 01:10:07 PM', '2019/12/31 11:00:06 AM')))); // 26:10:01

这段代码在date1和date2在同一时区时能够正确工作。但我建议您使用moment.js或其他库。

英文:

Just use the Date

const dateDiffMs = (date1,date2 ) =&gt; {
	const d1 = new Date(date1);
	const d2 = new Date(date2);
    return d1.getTime() - d2.getTime()
}

const ms2hms = (ms) =&gt; {
    const sec = Math.floor(ms / 1000)
    const min = Math.floor(sec / 60)
    const h = Math.floor(min / 60)

    return [
        h,
        min % 60,
	    sec % 60,
    ];
};

const format = (n) =&gt; n &lt; 10 ? &#39;0&#39; + n : n;

const hms2str = ([h, min, sec]) =&gt; {
    return `${h}:${format(min)}:${format(sec)}`
}

alert(hms2str(ms2hms(dateDiffMs(&#39;2020/01/01 01:10:07 PM&#39;, &#39;2019/12/31 11:00:06 AM&#39;)))); // 26:10:01

This code works correctly if both date1 and date2 are in the same timezone. But i would recommend you to use moment.js or some other library

答案2

得分: 0

I would do this by gathering the date in second since whenever computers decided to keep track of time for us sometime in the 70's (epoch). Then pass it the second value and subtract, leaving the difference.

You would then need to convert it back to a date format I presume:

(function(){
    var dateOneSeconds = new Date().getTime() / 1000;

    setTimeout(function(){
        var dateTwoSeconds = new Date().getTime() / 1000;
        var seconds = dateTwoSeconds - dateOneSeconds;
        console.log(seconds);
        var timeDifferenceInDate = new Date(seconds * 1000).toISOString().substr(11, 8);
        console.log(timeDifferenceInDate);
    }, 3000);
})();

NOTE: I have used a timeout function - you will already have two dates that do not match to pop in.

EDIT: having been notified the days will not be calculated, you could maybe use date to calculate your time in seconds then use Math to create your display:

(function(){
    var dateOneSeconds = new Date().getTime() / 1000;

    setTimeout(function(){
        var dateTwoSeconds = new Date().getTime() / 1000;
        var seconds = dateTwoSeconds - dateOneSeconds;
        console.log(seconds);

        seconds = Number(seconds);
        var d = Math.floor(seconds / (3600*24));
        var h = Math.floor(seconds % (3600*24) / 3600);
        var m = Math.floor(seconds % 3600 / 60);
        var s = Math.floor(seconds % 60);

        timeDifferenceInDate = d + ':' + h + ':' + m + ':' + s;

        console.log(timeDifferenceInDate);
    }, 3000);
})();
英文:

I would do this by gathering the date in second since whenever computers decided to keep track of time for us sometime in the 70's (epoch). Then pass it the second value and subtract, leaving the difference.

You would then need to convert it back to a date format I presume:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

(function(){

	var dateOneSeconds = new Date().getTime() / 1000;

	setTimeout(function(){

		var dateTwoSeconds = new Date().getTime() / 1000;

		var seconds = dateTwoSeconds - dateOneSeconds;

		console.log(seconds);

		var timeDifferenceInDate = new Date(seconds * 1000).toISOString().substr(11, 8);

		console.log(timeDifferenceInDate);

	}, 3000);

})();

<!-- end snippet -->

NOTE: I have used a timeout function - you will already have two dates that do not match to pop in.

EDIT: having been notified the days will not be calculated, you could maybe use date to calculate your time in seconds then use Math to create your display:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

(function(){

	var dateOneSeconds = new Date().getTime() / 1000;

	setTimeout(function(){

		var dateTwoSeconds = new Date().getTime() / 1000;

		var seconds = dateTwoSeconds - dateOneSeconds;

		console.log(seconds);

		/* var timeDifferenceInDate = new Date(seconds * 1000).toISOString().substr(11, 8); */

		seconds = Number(seconds);
		var d = Math.floor(seconds / (3600*24));
		var h = Math.floor(seconds % (3600*24) / 3600);
		var m = Math.floor(seconds % 3600 / 60);
		var s = Math.floor(seconds % 60);

		timeDifferenceInDate = d + &#39;:&#39; + h + &#39;:&#39; + m + &#39;:&#39; + s;

		console.log(timeDifferenceInDate);

	}, 3000);

})();

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月7日 00:38:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定