util中写个js方法
// 取当前时间的前一个周四
export const getThursday = () => {
let now = new Date();
let nowTime = now.getTime();
let today = now.getDay(); // 今天是星期几
let oneDayLong = 24*60*60*1000;
// 周五周六周日,展示本周四
let nowDay1 = new Date(nowTime - (today-4)*oneDayLong);
let year1 = nowDay1.getFullYear();
let month1 = nowDay1.getMonth()+1;
let day1 = nowDay1.getDate();
// 周一-周四,展示上周四
let nowDay2 = new Date(nowTime - (today+3)*oneDayLong);
let year2 = nowDay2.getFullYear();
let month2 = nowDay2.getMonth()+1;
let day2 = nowDay2.getDate();
let date = '';
if(today > 4 || today === 0) {
if(day1 < 10) {
date = year1 + '-' + month1 + '-' + '0' + day1;
} else {
date = year1 + '-' + month1 + '-' + day1;
}
} else {
if(day2 < 10) {
date = year2 + '-' + month2 + '-' + '0' + day2;
} else {
date = year2 + '-' + month2 + '-' + day2;
}
}
return date;
}
页面中引用
(多的不写了,在react中实现的)
// 头
import { DatePicker } from 'antd';
import * as util from '../../crm-assets/js/utils.js';
const { RangePicker } = DatePicker;
const dateFormat = 'YYYY-MM-DD';
// constructor函数,默认时间
constructor(props) {
super(props);
this.state = {
dateDefault: [moment(util.getThursday(), dateFormat), moment(util.getThursday(), dateFormat)],
}
}
// componentDidMount默认渲染
componentDidMount() {
moment.locale('zh-cn',{
day: {
dow: 5,
},
});
this.getData();
}
// 传值,getTable调用接口并返回
getData = async () => {
let date = util.getThursday();
this.setState({
startTime: date,
endTime: date,
},()=>{
this.getTable();
});
}
render() {
const { dateDefault } = this.state
return (<>
>)
}
今天是/12/18,星期五,前一个周四是昨天。选择器默认展示:
标签:DatePicker,const,day2,util,let,date,AntD,选择器,today
来源: /qq_40138556/article/details/111386502