I am attempting to retrieve the current time and implement it within a conditional if statement to check if it falls between certain set times. Although I followed the instructions in the documentation by using moment().isBetween(nine, ten)
, where nine and ten are strings, the moment() function returns both the day and time. In an alternative attempt with
moment().format("hh:mm A").isBetween(nine, ten)
, converting the output into a string prevents me from utilizing isBetween. How can I extract the current time correctly for this purpose? If employing the Date object in vanilla JS would simplify this task, please advise.
// attempted code examples
let nine = "9:00"
let nineFormat = moment(nine, "HH:mm A").format("hh:mm A");
let ten = "10:00"
let tenFormat = moment(ten, "HH:mm A").format("hh:mm A");
if(moment().isBetween(nineFormat,tenFormat) {
console.log("I'm here");
}
// another failed attempt
let test = moment().format("hh:mm A");
if(test.isBetween(nineFormat,tenFormat)) {
console.log("I'm here");
}
The issue encountered refers to example 1:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info. Arguments: [0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 09:00 AM, _f: undefined, _strict: undefined, _locale: [object Object] Error