gpt4 book ai didi

javascript - 如何检查时间是否大于javascript中两个日期之间的总时间?

转载 作者:行者123 更新时间:2023-12-03 08:37:48 25 4
gpt4 key购买 nike

我想检查时间 12.55(这是那里的格式,我无权编辑格式)是否大于 2 个日期之间的总时间,例如 2015-10-12 10:00"和 2015- 10-12 12:00。我已经完成了查找 diff 的第一部分,但不知道如何执行第二部分,即查找时间大于时差。希望有人可以帮助我!

var getDayTimeDiff = function(start, end){

var date1 = new Date(start);
var date2 = new Date(end);

var diff = date2.getTime() - date1.getTime();

var msec = diff;
var hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
var ss = Math.floor(msec / 1000);
msec -= ss * 1000;

return diff;
}



var timeDiff = getDayTimeDiff("2015-10-12 10:00","2015-10-12 12:00");
console.log(timeDiff); // 7200000


var time2Calc = "12.55";

最佳答案

您可以将时间转换为毫秒,然后比较以毫秒为单位的差异

var getDayTimeDiff = function(start, end) {
var date1 = new Date(start);
var date2 = new Date(end);
return date2.getTime() - date1.getTime();
}

var toMillis = function(time) {
var parts = time.split('.');
return (parts[0] * 60 + +parts[1]) * 60 * 1000;
}



function test(t1, t2) {
var timeDiff = getDayTimeDiff(t1, t2);


var time2Calc = "12.55";
var base = toMillis(time2Calc);

var result = (Math.abs(timeDiff) > base);
snippet.log(t1 + ' : ' + t2 + ' -> ' + result);
}

test("2015-10-12 10:00", "2015-10-12 23:00");
test("2015-10-12 10:00", "2015-10-12 12:00");
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

关于javascript - 如何检查时间是否大于javascript中两个日期之间的总时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33143408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com