目標是檢查,所選的日期是否超過最大時間。
我有一個時間區間的陣列。最大的時間是toMinutes - 20 minutes。
下面你可以找到我的解決方案,它總是回傳false.
如何編輯它以符合預期的結果,這也是在下面指定的?
。const dateRanges = [{fromMinutes: 360, toMinutes: 600}, {fromMinutes: 700, toMinutes: 900}]。
// "06:00 - 10:00", "11:40" - "15:00"
const minutesToHourFormat = (minutes) => {
return new Date(minutes * 1000)。 toISOString().slice(14, 19) 。
};
const isWithinMinDate = (/span>) => {
const selectedDate = "14:30"/span>;
return dateRanges.every(({ toMinutes }) => {
const minTime = minutesToHourFormat(toMinutes - 20)。
return selectedDate <= minTime;
})
}
//預期結果 return selectedDate <= minTime; }
//14:30 -> true
// 14:40 -> false
// 14:10 -> true[/span
// 9:50 -> false
// 9:30 -> true
console.log(isWithinMinDate(), 'res'/span>)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你的every呼叫應該針對兩個限制進行驗證,因為給定的時間可能在任何區間之外,或者當它在一個結束區間的20分鐘之內時,在針對后來的區間進行檢查時,它仍然被考慮!
const hourFormatToMinutes = (fmt) =>
fmt.split(":"/span>)。 reduce((h, m) => h * 60 m) 。
const minutesToHourFormat = (minutes) =>
new Date(minutes * 1000)。 toISOString().slice(14, 19)。
const dateRanges = [
{ fromMinutes: hourFormatToMinutes("6:00"/span>), toMinutes: hourFormatToMinutes("10:00") },
{ fromMinutes: hourFormatToMinutes("11:40"/span>), toMinutes: hourFormatToMinutes("15:00") }; ;toMinutes?
];
const isWithinMinDate = (hourFmt) => {
const minutes = hourFormatToMinutes(hourFmt)。
return dateRanges.some(({ fromMinutes, toMinutes }) =>/span>
minutes >= fromMinutes && minutes < toMinutes - 20
);
}
for (const test of ["14。 30", "14: 40", "14: 10", "9: 50", "9: 30"] ) {
console.log(test, isWithinMinDate(test))。
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/332775.html
標籤:
上一篇:如何在單元測驗中使用敏感資料?
