Element UI -- el-date-picker 日期组件设置默认值


最近有需求,列表查询条件的日期组件自动展示前一周到今天的日期。做个小记。废话不多说,直接上代码。

html:

<el-date-picker
         style="width:23%" 
         v-model="time"
         type="daterange"
         range-separator="至"
         start-placeholder="开始日期"
         end-placeholder="结束日期"
         value-format="yyyy-MM-dd"
></el-date-picker>

js:计算属性

 computed: {
        // 默认时间
        timeDefault () {
            let date = new Date()
            // 通过时间戳计算
            let defalutStartTime = date.getTime() - 7 * 24 * 3600 * 1000 // 转化为时间戳
            let defalutEndTime = date.getTime()
            let startDateNs = new Date(defalutStartTime) 
            let endDateNs = new Date(defalutEndTime)
            // 月,日 不够10补0
            defalutStartTime = startDateNs.getFullYear() + '-' + ((startDateNs.getMonth() + 1) >= 10 ? (startDateNs.getMonth() + 1) : '0' + (startDateNs.getMonth() + 1)) + '-' + (startDateNs.getDate() >= 10 ? startDateNs.getDate() : '0' + startDateNs.getDate())
            defalutEndTime = endDateNs.getFullYear() + '-' + ((endDateNs.getMonth() + 1) >= 10 ? (endDateNs.getMonth() + 1) : '0' + (endDateNs.getMonth() + 1)) + '-' + (endDateNs.getDate() >= 10 ? endDateNs.getDate() : '0' + endDateNs.getDate())
            return [defalutStartTime, defalutEndTime]
        }
    },

created生命周期里直接赋值

    created(){
        this.time = this.timeDefault;
    },

以上即可。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM