admin管理员组文章数量:1400583
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.
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}
/>
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.
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}
/>
Share
Improve this question
asked Feb 10, 2022 at 11:05
Chris MichaelChris Michael
3397 silver badges20 bronze badges
5
- whats the issue you are having – cmgchess Commented Feb 10, 2022 at 11:21
- @cmgchess The idea is that it is disabled for example, from 2pm onwards – Chris Michael Commented Feb 10, 2022 at 11:28
- so you want to disable future time? – cmgchess Commented Feb 10, 2022 at 11:32
- @cmgchess that’s correct! – Chris Michael Commented Feb 10, 2022 at 11:32
- can do this but there is a strange edge case which allows me to select a disabled hour – cmgchess Commented Feb 10, 2022 at 13:54
3 Answers
Reset to default 3If 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}
/>
);
};
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)
),
})}
/>
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'}
/>
本文标签: javascriptHow to disable time from now with time picker antdStack Overflow
版权声明:本文标题:javascript - How to disable time from now with time picker antd - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744269610a2598118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论