我正在尝试使用moment.js检查在下午4:29和上午8:59之间两次之间是否发生时间,但是它不起作用,这是我的代码:
var dropoff_date = new Date(document.getElementById("car-rental-dropoff-date").value);
var dropoff_time_string = document.getElementById("car-rental-dropoff-time").value;
var format = 'h:mm A';
var dropoff_time = moment(dropoff_time_string,format),
ahStart = moment('4:29 PM', format),
ahEnd = moment('8:59 AM', format);
if ((moment(dropoff_time).isBetween(ahStart, ahEnd)) {
alert ("it works!");
}
但是,如果我将上午8:59更改为下午9:00,则可以使用,但是如果我进入上午,则无法使用,有人可以帮我解决此问题吗?
编辑3:我刚刚开始工作,但是这段代码似乎有点多,如果有人有更好的方法,我将不胜感激:
var dropoff_time_string = document.getElementById("car-rental-dropoff-time").value;
var format = 'h:mm A';
var dropoff_time = moment(dropoff_time_string,format),
closingToday = moment('4:30 PM', format),
closingYesterday = moment('4:30 PM', format).subtract(1, 'day'),
openingToday = moment('9:00 AM', format),
openingTomorrow = moment('9:00 AM', format).add(1, 'day');
if (((moment(dropoff_time).isBetween(closingYesterday , openingToday)) || (moment(dropoff_time).isBetween(closingToday , openingTomorrow))) {
//bill = (bill+20000);
alert ("IT WORKS!" );
}
答案 0 :(得分:0)
我想分析器很挑剔。这种格式有效,加上您在时间变量上缺少一些var
声明,并且在行尾缺少一些分号:
var format = "MM-DD-YY hh:mm A";
var dropoff_time = moment("01-01-01 8:21 pm", format);
var ahStart = moment('01-01-01 4:30 pm', format);
var ahEnd = moment('01-02-01 8:30 am', format);
if (dropoff_time.isBetween(ahStart, ahEnd)) {
console.log("it works!");
}