Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查javascript中的时差_Javascript_Arrays - Fatal编程技术网

检查javascript中的时差

检查javascript中的时差,javascript,arrays,Javascript,Arrays,我有一个格式为“2021-02-25T12:30:00”的时间数组,只有当日期和时间至少提前24小时时,我才需要将它们放入一个新数组。我尝试了这段代码来检查条件,但出现了一些错误 date2=“2021-02-26T12:30:00” 函数getFormattedDate(){ 变量日期=新日期(); var str=date.getFullYear()+“-”+(date.getMonth()+1)+“-”+date.getDate()+“T”+date.getHours()+”:“+dat

我有一个格式为“2021-02-25T12:30:00”的时间数组,只有当日期和时间至少提前24小时时,我才需要将它们放入一个新数组。我尝试了这段代码来检查条件,但出现了一些错误

date2=“2021-02-26T12:30:00”
函数getFormattedDate(){
变量日期=新日期();
var str=date.getFullYear()+“-”+(date.getMonth()+1)+“-”+date.getDate()+“T”+date.getHours()+”:“+date.getMinutes()+”:“+date.getSeconds();
返回str;
}
if((Math.abs(新日期(date2.slice(-8))-新日期(getFormattedDate().slice(-8)))/36e5)>=24){
日志(“足够的时间”)
}否则{
console.log(“时间不够”)
}

console.log(date2.slice(-8))
您可以尝试以下方法

const hrs24=1000*60*60*24;//24小时毫秒
const date2=“2021-02-26T12:30:00”;
if(新日期(date2.getTime()-Date.now()>=hrs24)
console.log(“足够的时间”);
其他的
console.log(“时间不够”);

日期
可以表示为记号。将
date2
转换为
Date
,然后添加/减去表示24小时的刻度数。然后它是对
Date
的比较,而不是拆分和解析字符串@Jasen什么记号代表24小时?在我的例子中,它必须至少是24小时。你看到链接的问题了吗?是的,但问题不涉及时间范围,而是一个特定的时间