我正在构建一个约会计划程序,该计划具有使用下拉菜单选择一个人的下拉菜单。假设John Smith和flatpickr选择一个日期。当我选择John Smith时,他应该在10月30日下午3-5点之间进行约会。因此,如果我要显示和禁用按钮,则他在下午3-5点之间不可用,并显示其余按钮的可用时间。我该如何解决?我可以安装NPM吗?我要比较的时间是从js库开始的。我正在使用React Js,从那个人那里得到的时间是来自Ajax调用。
componentDidMount() {
let id = this.props.userId || this.props.match.params.id
const promise = userService.getUserIdNetwork(id)
promise.then(response => {
const coaches = []
const coachItems = response.item
coachItems.map(item => {
coaches.push({
value: item.connectionId
, label: `${item.firstName} ${item.lastName}`
})
this.setState({
options: coaches
, requesterId: id
})
})
})
// array of objects that have start AND end datetimes
//make the date with the appointment date
const startAndEndTimes = [
{startTime: moment().hour(12).minute(0).second(0), endTime: moment().hour(12).minute(59).second(59)}
, {startTime: moment().hour(1).minute(0).second(0), endTime: moment().hour(1).minute(59).second(59)}
, {startTime: moment().hour(2).minute(0).second(0), endTime: moment().hour(2).minute(59).second(59)}
, {startTime: moment().hour(3).minute(0).second(0), endTime: moment().hour(3).minute(59).second(59)}
, {startTime: moment().hour(4).minute(0).second(0), endTime: moment().hour(4).minute(59).second(59)}
, {startTime: moment().hour(5).minute(0).second(0), endTime: moment().hour(5).minute(59).second(59)}
, {startTime: moment().hour(6).minute(0).second(0), endTime: moment().hour(6).minute(59).second(59)}
, {startTime: moment().hour(7).minute(0).second(0), endTime: moment().hour(7).minute(59).second(59)}
, {startTime: moment().hour(8).minute(0).second(0), endTime: moment().hour(8).minute(59).second(59)}
, {startTime: moment().hour(9).minute(0).second(0), endTime: moment().hour(9).minute(59).second(59)}
, {startTime: moment().hour(10).minute(0).second(0), endTime: moment().hour(10).minute(59).second(59)}
, {startTime: moment().hour(11).minute(0).second(0), endTime: moment().hour(11).minute(59).second(59)}
, {startTime: moment().hour(12).minute(0).second(0), endTime: moment().hour(12).minute(59).second(59)}
]
console.log(startAndEndTimes)
const promise2 = appointmentsService.getAppointmentByDay(id, 10, 26, 7)
promise2.then(response => {
console.log(response)
response.items.forEach(timeObj => { //loops thru array of items to access appointmentDate by date. Coaches appointments
console.log(timeObj.appointmentDate)
debugger;
let appBegins = timeObj.appointmentDate;
let appEnds = timeObj.appointmentEndDate;
// get appEndTime as well
startAndEndTimes.forEach(arrTime => { // arr of times that will be compared to coaches times range
let slotBegins = arrTime.startTime._d
let slotEnds = arrTime.endTime._d
debugger;
if((appBegins > slotBegins) && (appBegins < slotEnds) || (appEnds < slotEnds) && (appEnds > slotBegins)){
return console.log("Not Available")
}else {
return console.log("Available Times")
}
})
this.setState({
dateFromApiCall: response.items
})
})
})
.catch(console.error)
}
答案 0 :(得分:0)
您可以使用moment-range
包(https://www.npmjs.com/package/moment-range/v/1.0.2)创建日期范围,然后检查当前日期是否在该范围内,以便可以禁用按钮:
const start = moment('2018-10-27 21:40:00');
const end = moment('2018-10-27 22:40:00');
const now = moment();
const range = moment().range(start, end);
range.contains(now); // Current time is within the range therefore disable button.