Chris Michael
Chris Michael

Reputation: 329

How to disable time from now with time picker antd

I would like to know how I can disable the hour and minutes, taking into consideration from the current time and not from past hours.

enter image description here

  const disabledHours = () => {
    const hours = [];
    for (let i = 0; i < moment().hour(); i += 1) hours.push(i);
    return hours;
  };

  const disabledMinutes = (selectedHour) => {
    const minutes = [];
    if (selectedHour === moment().hour()) {
      for (let i = 0; i < moment().minute(); i += 1) minutes.push(i);
    }
    return minutes;
  };
   <TimePicker
       disabledHours={disabledHours}
       disabledMinutes={disabledMinutes}
       format="h:mm a"
       style={{ width: '100%' }}
       disabled={isLoading}
   />

Upvotes: 3

Views: 7399

Answers (3)

Marnieeee
Marnieeee

Reputation: 1

Using dayjs and returning disabled past hours & minutes

const disabledTime = useCallback(() => {
    return {
        disabledHours: () => {
            const hours: number[] = [];
            const currentHour = dayjs().hour();

            for (let i = currentHour - 1; i >= 0; i--) {
                hours.push(i);
            }

            return hours;
        },
        disabledMinutes: selectedHour => {
            const minutes: number[] = [];
            const currMinute = dayjs().minute();

            if (selectedHour === dayjs().hour()) {
                for (let i = currMinute - 1; i >= 0; i--) {
                    minutes.push(i);
                }
            }

            return minutes;
        },
    };
}, []);

Setting it to the timepicker or other pickers, my example like

<TimePicker
    disabledTime={disabledTime}
    format={'HH:mm'}
/>

Upvotes: 0

Ahmed Korany
Ahmed Korany

Reputation: 51

I face this with a case

  • disabled all hours
  • enable minutes from 0 minutes to 12 minutes and disable it from 13 to 59

you can replace 12 with the current minutes - get it by new Date().getMinutes(); or by moments moment().minute(); or by dayJs dayjs().minute();

    <TimePicker
          disabledTime={() => ({
            disabledHours: () =>
              Array.from({ length: 24 }, (_, i) => (i >= 1 ? i : i - 1)),
            disabledMinutes: () =>
              Array.from({ length: 60 }, (_, i) =>
                i >= 12 + 1
                  ? i
                  : i - (12 + 1)
              ),
          })}
        />

time picker after disabled some minutes and all hours

Upvotes: 1

Anton Komyshan
Anton Komyshan

Reputation: 1581

If you want to disable future time:

const disabledHours = () => {
  const hours = [];
  const currentHour = moment().hour();

  for (let i = currentHour + 1; i <= 24; i++) {
    hours.push(i);
  }

  return hours;
};

const disabledMinutes = (selectedHour) => {
  const minutes = [];
  const currentMinute = moment().minute();
  if (selectedHour === moment().hour()) {
    for (let i = currentMinute; i <= 60; i++) {
      minutes.push(i);
    }
  }
  return minutes;
};

DEMO: https://codesandbox.io/s/antd-reproduction-template-forked-uslk0?file=/index.js

To avoid problems with switching AM or PM you can write any logic about how your want to set max time reference to onSelect callback, for example:

const TimeComponent = () => {
  const [selectedTime, setSelectedTime] = useState(moment());
  const disabledHours = () => {
    const hours = [];
    const currentHour = moment().hour();

    for (let i = currentHour + 1; i <= 24; i++) {
      hours.push(i);
    }

    return hours;
  };

  const disabledMinutes = (selectedHour) => {
    const minutes = [];
    const currentMinute = moment().minute();
    if (selectedHour === moment().hour()) {
      for (let i = currentMinute + 1; i <= 60; i++) {
        minutes.push(i);
      }
    }
    return minutes;
  };

  const onSelect = (time) => {
    if (time.isAfter(moment())) {
      console.log("ping");
      setSelectedTime(moment());
      return;
    }

    setSelectedTime(time);
  };

  return (
    <TimePicker
      onSelect={onSelect}
      disabledHours={disabledHours}
      disabledMinutes={disabledMinutes}
      format="h:mm a"
      value={selectedTime}
      style={{ width: "100%" }}
      use12Hours={true}
    />
  );
};

Upvotes: 3

Related Questions