简体   繁体   English

使用Moment JS的时间可用性

[英]Time availability using Moment JS

I'm building an appointment scheduler that has a dropdown using Dropdown Select to select a person. 我正在建立一个具有下拉菜单的约会调度程序,该下拉菜单使用下拉菜单选择来选择一个人。 Let's say John Smith and flatpickr to select a date. 假设John Smith和flatpickr选择一个日期。 When I select on John smith he should have an appointment between 3-5pm on Oct 30th. 当我选择John Smith时,他应该在10月30日下午3-5点之间进行约会。 So if I want to display and disable the button he's not available between 3-5pm and display the rest of the buttons with times he is available. 因此,如果我要显示和禁用按钮,则他在下午3-5点之间不可用,并显示其余按钮的可用时间。 How can I go tackle this? 我该如何解决? Is there a NPM I can install? 我可以安装NPM吗? The times I will be comparing to are from the moment js library. 我要比较的时间是从js库开始的。 I'm using React Js and the times I'm getting from that person is from an Ajax call. 我正在使用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)
  }

You can use moment-range package ( https://www.npmjs.com/package/moment-range/v/1.0.2 ) to create a range of dates and then check if current date is within that range so you can disable the button: 您可以使用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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM