使用antd中的DatePicker组件
const RangeDate = (name, code, required = true, initVal) => (
<Item label={name} name={code} rules={[{ required }]} initialValue={initVal}>
<DatePicker.RangePicker
ranges={{
'今天': [moment(), moment()],
'昨天-今天': [moment().subtract(1, 'days'), moment()],
'这个月': [moment().startOf('month'), moment().endOf('month')],
'上个月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
'今年': [moment().startOf('year'), moment().endOf('year')],
'去年': [moment().subtract(1, 'year').startOf('year'), moment().subtract(1, 'year').endOf('year')]
}}
/>
</Item>
);
values.date[0].format("YYYY-MM-DD HH:mm:ss"); //values.date[0]为选择的第一个时间 values.date[0].subtract("days",118).format("YYYY-MM-DD"); //values.date[0]时间的前118天时间 values.date[0].subtract("years",1).format("YYYY-MM-DD"); //values.date[0]时间的前1年时间 values.date[0].subtract("months",1).format("YYYY-MM-DD"); //values.date[0]时间的前1个月时间 values.date[0].subtract("weeks",1).format("YYYY-MM-DD"); //values.date[0]时间的前一个星期时间
选择今年时间:2022-01-01,2022-12-31
更新选择时间的前118天:2021-09-05,2022-09-04