如何在打字稿的日期陣列中檢查當前日期是否可用。嘗試使用以下代碼檢查,如果當前在陣列日期之間可用,則其給出的結果仍然為假。請建議我解決方案。
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);
uj5u.com熱心網友回復:
include 僅檢查陣列上的完全匹配,不包括 between
要使用包含,您必須在 2 之間的每個可能時間填充陣列,
但是您可以使用 > >= 和 < <= 來檢查
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
這是使用字母比較而不是日期比較,因此會產生奇怪的結果,您應該真正轉換為時間戳或比較日期組件,以便正確比較,例如
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)
uj5u.com熱心網友回復:
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 ?true:false;
console.log(status);
以這種方式嘗試,includes就像indexOf陣列的函式一樣,它不會在其中檢查檢查是否與正在使用的陣列的該值相同的匹配項
uj5u.com熱心網友回復:
let myTimes = ['5:00:00 PM', '5:59:59 PM'];
const fooTime = '5:30:00 PM';
const fooIsBetween = fooTime > myTimes[0] && fooTime < myTimes[1]; // true
如果您還使用當前時間而不是 fooTime:
const now = new Date().toLocaleTimeString('en-US');
uj5u.com熱心網友回復:
這里的許多其他答案都使用基于字串的比較。為了確保正確的結果,您應該改用數字比較。尤其是在晚上 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
如果要對許多專案執行此檢查,則應改為使用 currying 通過預先計算要比較的范圍來擠出一些額外的性能。
function buildCheckTime(startTime, endTime) {
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();
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487925.html
標籤:javascript 打字稿
