如何检查typescript中日期数组中当前日期是否可用



如何检查typescript中日期数组中当前日期是否可用。尝试用下面的代码检查,如果current在数组日期之间可用,其给出的结果仍然为false。请给我一个建议。

let time1= ['5:00:00 PM','5:59:59 PM'];
const dateCheck = new Date().toLocaleTimeString('en-US');
const status = time1.includes(dateCheck) ?true:false;
console.log(status);

include只检查数组的精确匹配,不包括between

如果要使用include,则必须使用2、

之间的所有可能时间填充数组。但是你可以使用>>= and <<=检查

const dateCheck:date = new Date().toLocaleTimeString('en-US');
const time1:string[] = ['12:00:00 PM','12:59:59 PM'];
const status: boolean = (time1[0]<=dateCheck && time1[1]>=dateCheck)
console.log(dateCheck,status);
//output "12:56:13 PM", true

这是使用字母比较而不是日期比较,所以会产生奇怪的结果,你应该真正转换为时间戳或比较日期组件,以正确比较eg

const dateCheck:date = new Date()
const time1 = [{
hour:12,
minute:0
},{
hour:17,
minute:59
}]
//convert to number of minutes since midnight so you can use a numeric compare
const dayMins = dateCheck.getHours()*60+ dateCheck.getMinutes()
const start = time1[0].hour*60+ time1[0].minute
const end = time1[1].hour*60+ time1[1].minute
const status = (
dayMins>=start &&  dayMins<=end
)
console.log(dateCheck.toLocaleTimeString(),status)

let times = ['5:00:00 PM', '5:59:59 PM'];
startTime = new Date('2022-01-01 ' + times[0]).getTime()
endTime = new Date('2022-01-01 ' + times[1]).getTime()

testTime = new Date('2022-01-01 ' + '5:30:00 PM').getTime()
const isBetween = testTime > startTime && testTime < endTime;

这里的许多其他答案使用基于字符串的比较。为了确保正确的结果,您应该使用数值比较。特别是在不同的日子之间,比如晚上10点到凌晨2点之间查看。

function checkTime(timeCheck, timeRange) {
const datePart = '2000-01-01 ', // used to anchor times to the same date
d0 = new Date(datePart + timeRange[0]).getTime(),
d1 = new Date(datePart + timeRange[1]).getTime(),
dToCheck = new Date(datePart + (typeof timeCheck === 'string' ? timeCheck : timeCheck.toLocaleTimeString('en-US'))).getTime();
if (isNaN(d0) || isNaN(d1) || isNaN(dToCheck)) throw new TypeError('invalid time format');
return d0 < d1
? d0 <= dToCheck && dToCheck <= d1  // handle times on the same day
: d1 <= dToCheck || dToCheck <= d0; // handle times on different days
}
const timeRange = ['5:00:00 PM','5:59:59 PM'];
checkTime('5:15:00 PM', timeRange); // true
checkTime('5:15 PM', timeRange); // true
checkTime('5:15:00 AM', timeRange); // false
checkTime('5:15 AM', timeRange); // false
checkTime(new Date().toLocaleTimeString('en-US'), timeRange); // depends
checkTime(new Date(), timeRange); // depends
checkTime(new Date('2021-01-01T12:00:00Z'), timeRange); // depends on timezone

如果您希望对许多项执行此检查,则应该使用curry来通过预先计算要比较的范围来挤出一些额外的性能。

function buildCheckTime(startTime, endTime) {
const datePart = '2000-01-01 ', // used to anchor times to the same date
d0 = new Date(datePart + startTime).getTime(),
d1 = new Date(datePart + endTime).getTime();
if (isNaN(d0) || isNaN(d1)) throw new TypeError('invalid time format');
return d0 < d1
? function(timeCheck) { // handle times on the same day
const dToCheck = new Date(datePart + (typeof timeCheck === 'string' ? timeCheck : timeCheck.toLocaleTimeString('en-US'))).getTime();
if (isNaN(dToCheck)) throw new TypeError('invalid time format');
return d0 <= dToCheck && dToCheck <= d1;
}
: function (timeCheck) { // handle times on different days
const dToCheck = new Date(datePart + (typeof timeCheck === 'string' ? timeCheck : timeCheck.toLocaleTimeString('en-US'))).getTime();
if (isNaN(dToCheck)) throw new TypeError('invalid time format');
return d1 <= dToCheck || dToCheck <= d0;
}
}
const checkTime = buildCheckTime('5:00:00 PM','5:59:59 PM');
checkTime('5:15:00 PM'); // true
checkTime('5:15 PM'); // true
checkTime('5:15:00 AM'); // false
checkTime('5:15 AM'); // false
checkTime(new Date().toLocaleTimeString('en-US')); // depends
checkTime(new Date()); // depends
checkTime(new Date('2021-01-01T12:00:00Z')); // depends on timezone
let time1= ['5:00:00 PM','5:59:59 PM'];
const dateCheck = new Date().toLocaleTimeString('en-US');
const status = time1[0] < dateCheck && time1[1] > dateCheck;
console.log(status);

尝试这样做,因为includes就像数组的indexOf函数它不会检查它是否与所使用的数组的值相同

相关内容

最新更新