如图:当前日期:9号;
正确应该是这样婶儿的: image.png
import React, { useState } from 'react';
import 'antd/dist/antd.css';
import { DatePicker, Space } from 'antd';
import type { RangePickerProps } from 'antd/es/date-picker';
import moment from 'moment';
const { RangePicker } = DatePicker;
const App = () => {
const [valueDate, setvalueDate] = useState('');
const onChange = (e) => {
console.log(moment(e).format('YYYY-MM-DD'), 'kjk');
setvalueDate(moment(e).format('YYYY-MM-DD'));
};
const onChanges = (e) => {
setvalueDate('');
};
const disabledDate: RangePickerProps['disabledDate'] = (current) => {
return current && current > moment().startOf('day');
};
const disabledDate1: RangePickerProps['disabledDate'] = (current) => {
return current && current > moment().subtract(1, 'days').endOf('day');
};
return (
<Space direction="vertical" size={12}>
错误方式:
<DatePicker
disabledDate={disabledDate}
value={valueDate ? moment(valueDate, 'YYYY-MM-DD') : null}
onChange={onChange}
/>
<br />
正确方式:
<DatePicker disabledDate={disabledDate1} onChange={onChanges} />
</Space>
);
};
export default App;
扩展
// 当天之前的日期不可选
const disabledDate = (current) => {
return current && current < moment().subtract(1, 'days'); //当天之前的不可选,不包括当天
//return current && current < moment().endOf('day');当天之前的不可选,包括当天
};
// 当天之后的日期不可选
const disabledDate = (current) => {
return current.getTime() > Date.now();
};
// 限制只能选今天及之前的日期(今天可选中)
const disabledDate = (current) => {
return current && current > moment().endOf('day');
};
// 限制只能选昨天及之前的日期(今天不可选中)
const disabledDate = (current) => {
return current && current > moment().subtract(1, 'days').endOf('day');
};
// 当天日期不许被选择,双休不能被选择
const disableDate = (date) => {
return (
(date && date < moment().endOf('day')) ||
date.isWeekday() == 6 ||
date.isWeekday() == 7
);
};
今夜MVP🏆🏆🏆
限制只能选昨天及之前的日期(今天不可选中)
current && current > moment().subtract(1, 'days').endOf('day');
网友评论