我们再用el进行开发时使用日期选择器的时候会遇到设置默认事件的需求,我的解决方式在一个js文件里设置好时间格式 例如: 格式为yyyy-mm-dd
const base = {
/**
* 返回日期格式 为 yy-mm--dd
*/
formatDateS(date) {
var YY = date.getFullYear();
var MM = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return `${YY}-${MM}-${DD}`;
},
}
然后在需要设置默认日期的页面引入该js 并设置默认时间段
import base from "@/utils/base";
const invoice_end = new Date();//结束时间
const invoice_start = new Date();//开始时间
invoice_start.setTime(invoice_start.getTime() - 3600 * 1000 * 24 * 29);/默认时间段
在日期的对应字段设置默认值就完成日期默认时间段的操作了(我这里设置默认事件为一个月 按三十天计算)