一、element框架時間選擇器,默認顯示起止時間為:當前日期往前推7天
1、html結構:
<el-form :inline="true" :model="deviceFormData" ref="deviceFormData" class="hardware-form"> <el-form-item label="查詢時間:"> <el-date-picker :editable="false" :clearable="false" v-model="deviceFormData.time" type="daterange" range-separator="至" start-placeholder="開始日期" end-placeholder="結束日期" :picker-options="pickerOptions" format="yyyy-MM-dd" value-format="yyyy-MM-dd"> </el-date-picker> </el-form-item> </el-form>
2、js部分:
data(){ return { deviceFormData:{ time:[] }, //不能選擇當前日期之后的時間 pickerOptions: { disabledDate(time) { return time.getTime() > Date.now(); } }, } } created() { if (process.client) { const that = this; that.getTimeFn(); } }, /** * 設置默認時間 */ getTimeFn() { const that = this; const end = new Date(); const start = new Date(); start.setTime(start.getTime() - 3600 * 1000 * 24 * 7); that.deviceFormData.time[0] = that.formatDate(start); that.deviceFormData.time[1] = that.formatDate(end); }, /** * 格式化時間 */ formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth() + 1; var myweekday = date.getDate(); if (mymonth < 10) { mymonth = "0" + mymonth; } if (myweekday < 10) { myweekday = "0" + myweekday; } return myyear + "-" + mymonth + "-" + myweekday; },
3、效果預覽: