gpt4 book ai didi

javascript - for循环中的时间比较

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

我遇到了一个奇怪的问题。下面是在 Javascript 中创建不同时间段的代码:

function secondsToTime(secs) {
var hours = Math.floor(secs / (60 * 60));
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12;

var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);

var userfriendlytime = ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) + " " + ampm;

return userfriendlytime;
}



$(document).ready(function() {

var ptc_id = "236051";
var ptc_date = "04/30/2021";
var ptc_from_time = "5400";
var ptc_to_time = "46800";
var ptc_duration = "07";
var ptc_duration_in_seconds = parseFloat(ptc_duration) * 60;
var ptc_passing_time = "03";
var ptc_passing_time_in_seconds = parseFloat(ptc_passing_time) * 60;

var timefrom = ptc_from_time;
var timeto = ptc_to_time;

var slottimeto = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

for (time = timefrom; time <= timeto;) {

var slotfromtime = parseFloat(timefrom);
var slottotime = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

var a_id = slotfromtime + "" + slottotime;

var slottime = secondsToTime(slotfromtime) + " - " + secondsToTime(slottotime)

console.log(slottime);
timefrom = parseFloat(slottotime) + parseFloat(ptc_passing_time_in_seconds);
time = timefrom;

if (parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds) > timeto) {
break;
}

}

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我以秒为单位存储时间。上面的代码不起作用,但是当我不时更改时间(例如减少时间)时,代码开始起作用。这是工作代码:

function secondsToTime(secs) {
var hours = Math.floor(secs / (60 * 60));
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12;

var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);

var userfriendlytime = ('0' + hours).slice(-2) + ":" + ('0' + minutes).slice(-2) + " " + ampm;

return userfriendlytime;
}



$(document).ready(function() {

var ptc_id = "236051";
var ptc_date = "04/30/2021";
var ptc_from_time = "10800";
var ptc_to_time = "46800";
var ptc_duration = "07";
var ptc_duration_in_seconds = parseFloat(ptc_duration) * 60;
var ptc_passing_time = "03";
var ptc_passing_time_in_seconds = parseFloat(ptc_passing_time) * 60;

var timefrom = ptc_from_time;
var timeto = ptc_to_time;

var slottimeto = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

for (time = timefrom; time <= timeto;) {

var slotfromtime = parseFloat(timefrom);
var slottotime = parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds);

var a_id = slotfromtime + "" + slottotime;

var slottime = secondsToTime(slotfromtime) + " - " + secondsToTime(slottotime)

console.log(slottime);
timefrom = parseFloat(slottotime) + parseFloat(ptc_passing_time_in_seconds);
time = timefrom;

if (parseFloat(timefrom) + parseFloat(ptc_duration_in_seconds) > timeto) {
break;
}

}

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果您注意到了,我刚刚更改了 ptc_from_time

最佳答案

您的程序将无法正常工作,因为您正在使用字母数字值来执行本应为数字的比较:

time <= timeto

确保这些变量始终包含数值。此外,无需使用将立即转换为数字的字母数字值。使您的设计尽可能简单:使用数字常量和值。

关于javascript - for循环中的时间比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67190104/

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