Javascript 如何获取当前的上课时间和剩余时间?

Javascript 如何获取当前的上课时间和剩余时间?,javascript,date,time,Javascript,Date,Time,我有这个功能: 函数getInfoSchoolTime(){ 变量日期=新日期(); var schoolBellTime=[“8:10”、“9:02”、“9:54”、“9:59”、“10:51”、“11:43”、“11:58”、“12:48”、“13:35”、“13:40”、“14:10”、“15:02”、“15:54”]; var剩余时间,当前小时; 对于(变量i=0;i=date.getHours()&&parseInt(startTime[1])>=date.getMinutes())

我有这个功能:

函数getInfoSchoolTime(){ 变量日期=新日期(); var schoolBellTime=[“8:10”、“9:02”、“9:54”、“9:59”、“10:51”、“11:43”、“11:58”、“12:48”、“13:35”、“13:40”、“14:10”、“15:02”、“15:54”]; var剩余时间,当前小时; 对于(变量i=0;i=date.getHours()&&parseInt(startTime[1])>=date.getMinutes())
if(parseInt(endTime[0])顶部的函数:
setDateTime()
获取日期和时间,并为该时间构造一个日期对象

然后我更新了您的函数,将
start
end
转换为当天的时间,然后检查它们之间是否存在
date.getTime()
。然后我只需从
end
中减去
date.getTime()
,并将结果从毫秒转换为分钟

var setDateTime=函数(日期,str){
var sp=str.split(“:”);
设置时间(parseInt(sp[0],10));
date.setMinutes(parseInt(sp[1],10));
返回日期;
}
函数getInfoSchoolTime(){
变量日期=新日期();
var schoolBellTime=[“8:10”,“9:02”,“9:54”,“9:59”,“10:51”,“11:43”,“11:58”,“12:48”,“13:35”,“13:40”,“14:10”,“14:10”,“15:02”,“15:54”];
var剩余时间、currentHour、currentPeriod;
对于(var i=0;istart.getTime()&&date.getTime()console.log(getInfoSchoolTime())
对于代码和API,这里有一种稍微不同的方法。它使用了两个辅助函数。每个函数都应该通过一个例子来说明:
pad(7)/=>“07”
对(['foo',bar',baz',qux'])/=>[[foo',bar',['bar',baz',['baz',qux']

main函数获取一个bell时间列表,并返回一个函数,该函数本身接受一个date对象,并返回您要查找的输出类型(period,period中的剩余时间)。此API使测试更加容易

const pad=nbr=>('00'+nbr).slice(-2)
const pairs=vals=>vals.reduce((res,val,idx)=>idx<1?res:res.concat([[vals[idx-1],val]]),[]))
const schoolPeriods=(schoolBellTime)=>{
常数减去时间=(t1,t2)=>60*t1.hour+t1.minute-(60*t2.hour+t2.minute)
const periods=pairs(schoolBellTime.map(time=>({hour:time.split(':')[0],minute:+time.split(':')[1]})))
返回日期=>{
const current={hour:date.getHours(),minute:date.getMinutes()}
如果(减去时间(当前,周期[0][0])<0){
返回{message:'before school day'}
}
如果(减去时间(当前,周期[periods.length-1][1])>0){
返回{信息:“放学后”}
}
const idx=periods.findIndex(period=>subtractTimes(当前,period[0])>=0&&subtractTimes(period[1],当前)>0)
常数周期=周期[idx]
返回{
电流:idx+1,
当前时段:`${period[0]。小时:${pad(period[0].minute)}-${period[1]。小时:${pad(period[1].minute)}`,
剩余:减去次数(周期[1],当前)
}
}
}
const getPeriod=学校上课时间([“8:10”、“9:02”、“9:54”、“9:59”、“10:51”、“11:43”、“11:58”、“12:48”、“13:35”、“13:40”、“14:10”、“14:10”、“15:02”、“15:54”)
console.log(“使用当前时间”)
console.log(getPeriod(new Date()))
log(“使用固定时间”)

console.log(getPeriod(新日期(2017、11、22、14、27))//圣诞假期会来吗?
下面是一个使用解构(
const[a、b]=[1,2]
)、数组映射、数组缩减、部分应用程序(闭包)和胖箭头函数语法的ES6示例

这在较旧的浏览器中可能不起作用

//将日期和钟声时间传递给函数,以便您可以更轻松地测试它
//您可以部分地应用bellTimes
const getInfoSchoolTime=bellTimes=>{
//将小时和分钟转换为数字
const convertedBellTimes=bellTimes
.map(bellTime=>bellTime.split(“:”)//分割小时和分钟
.map(([hour,minute])=>[new Number(hour),new Number(minute)]//转换为数字
.map(([hour,minute])=>(hour*60)+分钟//创建单个数字(hour*60)+分钟
.reduce(//用next压缩
(ret、item、index、all)=>
(index!==all.length-1)//不要做最后一个,创建[1,2][2,3][3,4]。。。
返回混凝土([[项目,全部[索引+1]])
:ret,
[]
);
返回日期=>{
//将传入日期转换为数字(小时*60)+分钟
const passedInTime=(date.getHours()*60)+date.getMinutes();
return convertedBellTimes.reduce(
([ret,goOn],[low,high],index,all)=>
//如果goOn为true,并且在当前和下一个钟形项之间经过时间
(goOn&&passedTime=低)
?[//找到项,返回对象并将goOn设置为false
{
当前:索引+1,
当前期间:钟楼时间[索引]+“-”+钟楼时间[索引+1],
剩余时间:高时间
},
false//将goOn设置为false,不继续检查
]
:[ret,goOn],//继续查找或继续跳过(如果goOn为false)
[
{current:0,currentPeriod:“学校出局”,剩余:0},//默认值
true//goOn的初始值
]
)[0];//减少为多个值(值,继续)只需要值
}
};
//一些测试
const date=新日期();
//部分应用于一些钟声时间
const schoolTime=getInfoSchoolTime(
[
"8:10", "9:02", "9:54", "9:59", "10:51",
"11:43",