gpt4 book ai didi

javascript - Javascript时间与给定时间进行比较

转载 作者:行者123 更新时间:2023-11-28 18:52:42 25 4
gpt4 key购买 nike

好吧,我无法理解这个问题,我在 SF 上看了很多帖子,但无法弄清楚。
我需要将服务器时间与硬编码时间进行比较。

我已经尝试过

            var breackfastCutOffTime    = "5:30";
var lunchCutOffTime = "10:00";
var dinnerCutOffTime = "17:00";


var serverTime = currentDate.getHours()+ ":" + currentDate.getMinutes();

if(Date.parse(toDay) == Date.parse(selectedDate)){
if(serverTime >= breackfastCutOffTime ){
document.getElementById("breakfast").disabled = true;
}else if(serverTime >= lunchCutOffTime){
document.getElementById("lunch").disabled = true;
}else if(serverTime >= dinnerCutOffTime){
document.getElementById("dinner").disabled = true;
}
}

我知道这无法比较,因为时间是文本格式。请有人帮助我完成这个。

最佳答案

您可以编写一个函数,仅将时间(hh:mm)转换为分钟(mm)。然后你可以比较两次并进行相应的操作。

伪代码:

function convert(string time) int {
//split the time by ':'
//convert the string hh,mm to int hour,mm
//calculate total minutes
//return total minutes
}

关于javascript - Javascript时间与给定时间进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34218104/

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