使用el-date-picker 設置默認日期和時間范圍


 


首先需要了解一下 new Date() 這個方法
var d = new Date();
d .getFullYear(); //獲取完整的年份(4位)
d .getMonth(); //獲取當前月份(0-11,0代表1月)
d .getDate(); //獲取當前日(1-31)
d .getDay(); //獲取當前星期X(0-6,0代表星期天)
d .getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)
d .getHours(); //獲取當前小時數(0-23)
d .getMinutes(); //獲取當前分鍾數(0-59)
d .getSeconds(); //獲取當前秒數(0-59)
d .getMilliseconds(); //獲取當前毫秒數(0-999)
d .toLocaleDateString(); //獲取當前日期
var mytime=d.toLocaleTimeString(); //獲取當前時間
d.toLocaleString( ); //獲取日期與時間

2.然后在我們的工程中引入el-date-picker 控件

<el-date-picker
v-model="value1"
type="datetimerange"
range-separator="至"
start-placeholder="開始日期"
end-placeholder="結束日期"
value-format="yyyy-MM-dd HH:mm"
>
</el-date-picker>
1

3.寫上我們的邏輯

export default {
data(){
return{
value1:[],
}
}
created() {
//當前設定的日期時間
let d = new Date
let year1,month1,day1;
[year1,month1,day1] = [d.getFullYear(),d.getMonth(),d.getDate()]
let date1 = new Date(year1, month1, day1,7)
this.value1.push(date1)
//前一天設定的日期時間
let year2,month2,day2
d.setTime(d.getTime()-24*60*60*1000);
[year2,month2,day2] = [d.getFullYear(),d.getMonth(),d.getDate()]
let date2 = new Date(year2,month2,day2,7)
this.value1.unshift(date2)
},
}

這樣我們就能得到一個我們想要效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM