我正在使用 moment.js 庫,如果我想檢查當前時間是否小于 11:00,我該如何實作?
let todaysDate = moment() // "yyyy-mm-dd"
var time = "22:59"; // set condition to just before 11:00
var elevenOclockTimeCheck = moment(todaysDate).startOf(time);
// check to see if time is equal or greater to 11:00 PM
console.log(elevenOclockTimeCheck);
// If it is before 11:00 then execte if block
if (todaysDate < elevenOclockTimeCheck) {
}
uj5u.com熱心網友回復:
您可以使用以下代碼:
moment().isBefore(moment({ hour:11, minute: 0}));
uj5u.com熱心網友回復:
請注意,您不需要 moment.js - 直接 js 日期/時間操作將完成它。
const todaysDate = new Date();
const hours = todaysDate.getHours();
const isBefore11 = hours <= 11;
const result = `The time ${isBefore11 ? 'is' : 'is not'} before 11`;
console.log(result);
// my current time is 11:02pm (23:02)- so this consoles 'The time is not before 11'
uj5u.com熱心網友回復:
只需呼叫.hourgetter 并進行比較
moment().hour() < 11;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/468976.html
標籤:javascript if 语句 时刻
上一篇:根據另一列中的值獲取每列的平均值
下一篇:結合更多的IF(或谷歌表格公式
