先封装函数,功能为:判断一个时间是否在某个时间段内:
function isTimes(newdate, startdate, enddate) {
var newdate = new Date(newdate);
var startdate = new Date(startdate);
console.log(newdate);
console.log(startdate);
var enddate = new Date(enddate);
var a = newdate.getTime() - startdate.getTime();
var b = newdate.getTime() - enddate.getTime();
console.log(a);
console.log(b);
if (a < 0 || b > 0) {
return '不在当前时间段内';
} else {
return '在当前时间段内';
}
}
接下来调用封装好的函数实现对应的需求:
console.log(isTimes('2020-03-11 00:00:01','2020-03-11 00:00:01','2020-05-10 10:10:12')); //在当前时间段内
如果感觉对自己有帮助,麻烦点一下关注,会一直和大家分享知识的,谢谢!!!