1. 禁用时间段
<RangePicker
disabledDate={this.disabledDate}
/>
// 当天之前的日期不可选
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;
}
2. 选择不超过7天的范围
import { DatePicker } from 'antd';
import React, { useState } from 'react';
const { RangePicker } = DatePicker;
const App = () => {
const [dates, setDates] = useState(null);
const [value, setValue] = useState(null);
const disabledDate = (current) => {
if (!dates) {
return false;
}
const tooLate = dates[0] && current.diff(dates[0], 'days') > 7;
const tooEarly = dates[1] && dates[1].diff(current, 'days') > 7;
return !!tooEarly || !!tooLate;
};
const onOpenChange = (open) => {
if (open) {
setDates([null, null]);
} else {
setDates(null);
}
};
return (
<RangePicker
value={dates || value}
disabledDate={disabledDate}
onCalendarChange={(val) => setDates(val)}
onChange={(val) => setValue(val)}
onOpenChange={onOpenChange}
onBlur={() => console.log('blur has been triggered')}
/>
);
};
export default App;
3. 汉化
1. dayjs
import React from 'react';
import { DatePicker } from 'antd';
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
const { RangePicker } = DatePicker;
// 这个可以不写
// dayjs.locale('zh-cn');
const App = () => {
return (
<RangePicker />
);
};
export default App;
2. moment
import React from 'react';
import { DatePicker } from 'antd';
import moment from 'moment';
import 'moment/locale/zh-cn';
const { RangePicker } = DatePicker;
// 这个可以不写
// moment.locale('zh-cn');
const App = () => {
return (
<RangePicker />
);
};
export default App;