gpt4 book ai didi

javascript - 使用javascript、jquery的时差

转载 作者:行者123 更新时间:2023-11-28 03:24:09 25 4
gpt4 key购买 nike

我正在尝试找出时差并打印出来。

我采用了两个日期字符串,并使用数学函数并将输出打印到

下面的代码给出了 NaN Nan:Nan

function CalcTimer(StartTiime, EndTime, NodetoDisplay) {
var Start = new Date(StartTiime).getTime();
var EndTime = new Date(EndTime).getTime();
var Diff = StartTiime - EndTime;
var days = Math.floor(Diff / (1000 * 60 * 60 * 24));
var hours = Math.floor((Diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((Diff % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((Diff % (1000 * 60)) / 1000);
NodetoDisplay.html(days+" "+hours+":"+minutes+"");
}

var addTimeDiffCalculator = {
InitializeThis : function() {
addTimeDiffCalculator.SetUpElements();
addTimeDiffCalculator.SetupBehaviour();
},
SetUpElements : function() {
var StartDate = $('<input type="text" id="StartDate">');
var EndDate = $('<input type="text" id="EndDate">');
$("#EndDate").datepicker();
var StartHr = $('<select id="StartHr">');
for(var SH=0; SH<=23; SH++){
$('<option />', {value: SH, text: SH}).appendTo(StartHr);
}
var EndHr = $('<select id="EndHr">');
for(var EH=0; EH<=23; EH++){
$('<option />', {value: EH, text: EH}).appendTo(EndHr);
}
var StartMin = $('<select id="StartMin">');
for(var SM=0; SM<=59; SM++){
$('<option />', {value: SM, text: SM}).appendTo(StartMin);
}
var EndMin = $('<select id="EndMin">');
for(var EM=0; EM<=59; EM++){
$('<option />', {value: EM, text: EM}).appendTo(EndMin);
}
var CalcButton = $('<button id="CalcButton">Calculate!</button>');
var p1 = $('<p id="p1"> Time Diff Calculator </p>'),
p2 = $('<p id="p2">'),
p3 = $('<p id="p3">'),
p4 = $('<p id="p4">'),
p5 = $('<p id="p5">');
StartDate.appendTo(p2);
StartHr.appendTo(p2);
StartMin.appendTo(p2);
EndDate.appendTo(p3);
EndHr.appendTo(p3);
EndMin.appendTo(p3);
CalcButton.appendTo(p4);
var TimeDiv = $("<div>");
p1.appendTo(TimeDiv);
p2.appendTo(TimeDiv);
p3.appendTo(TimeDiv);
p4.appendTo(TimeDiv);
p5.appendTo(TimeDiv);
TimeDiv.appendTo("body");
$("#StartDate").datepicker(); $("#EndDate").datepicker();
},
SetupBehaviour : function() {
var StDate = $("#StartDate").val(),
EnDate = $("#EndDate").val(),
StHr = $("#StartHr").val(),
EnHr = $("#EndHr").val(),
Stmin = $("#StartMin").val(),
Enmin = $("#EndMin").val();
var StartString = StDate+" "+StHr+":"+Stmin+":00",
EndString = EnDate+" "+EnHr+":"+Enmin+":00";

$("#CalcButton").click(function(){
CalcTimer(StartString,EndString,$("#p5"));
});
},
};

$(document).ready(function(){
addTimeDiffCalculator.InitializeThis();
});

我不明白哪里出了问题。我希望这是足够的信息来理解我的问题。

最佳答案

这里:

var Diff = StartTiime - EndTime;

你的意思是:

var Diff = Start - EndTime;

但它可能应该是:

var Diff = EndTime - Start;

关于javascript - 使用javascript、jquery的时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58828212/

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