一、代碼:
(1)控件
1 <span class="filterBlock"> 2 售票時間: 3 <DatePicker v-model="saleDate" 4 format="yyyy/MM/dd" 5 type="daterange" 6 placement="bottom-end" 7 placeholder="請選擇" 8 class="filter" 9 :value="saleDate"></DatePicker> 10 </span>
(2)默認值
1 data() { 2 return { 3 saleDate: '', 4 }; 5 }
(3)默認日期取得
1 mounted() { 2 const myDate = new Date(); 3 const year = myDate.getFullYear(); // 獲取當前年份 4 const month = myDate.getMonth() + 1; // 獲取當前月份(0-11,0代表1月所以要加1); 5 const day = myDate.getDate(); // 獲取當前日(1-31) 6 // 日期格式:2019/07/29 - 2019/07/29 7 this.saleDate = `${year}/${month}/${day} - ${year}/${month}/${day}`; 8 },
二、總結:
綁定屬性value,給value的值就是顯示在日期框中的值
注意:你拼接的當前日期格式要和控件的日期格式一模一樣,否則不顯示
三、結果顯示:
四、超簡單方法:
new Date()得出的是中國標准時間,但是iview可以自動轉換成要顯示的格式狀態
1 //調用方法 2 mounted() { 3 this.getDates(); 4 },
1 //寫方法 2 methods: { 3 getDates() { 4 const now = new Date(); 5 const nowdate = [now, now]; 6 this.saleDate = nowdate; 7 }, 8 }