我想检查当前时间是否在 11:00 和 moment.js 之前。我怎样才能做到这一点?
I would like to check if the current time is before 11:00 with moment.js. How can I achieve this?
我正在使用 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) {
}
您可以使用以下代码:
moment().isBefore(moment({ hour:11, minute: 0}));
请注意,您不需要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'
只需调用 .hour
getter 并比较
moment().hour() < 11;
我正在使用 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) {
}
您可以使用以下代码:
moment().isBefore(moment({ hour:11, minute: 0}));
请注意,您不需要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'
只需调用 .hour
getter 并比较
moment().hour() < 11;