上代码
html
<el-date-picker
v-model="datePicker"
type="date"
placeholder="选择日期">
</el-date-picker>
js
data() {
retutrn {
datePicker: [],
}
},
methods: {
getDefaultTime() {
let nowTime = new Date(); // 这个当前时间
let timeStamp = nowTime.getTime(); // 这个是时间戳;
let times= [timeStamp, timeStamp]; // 举例: 两个一样的时间戳
this.datePicker = [];
times.forEach(tiemItem => {
let item = new Date(Number(tiemItem)).getTimeStr();
this.datePicker.push(item);
})
console.log('this.datePicker: ', this.datePicker);
// 成功设置datePicker的时间
},
},
utils 日期配置
Date.prototype.getTimeStr = function () {
let value = this.getTime();
if(!value){
return undefined;
}
var time = new Date(value);
var year = time.getFullYear(); //得到年份
var month = time.getMonth(); //得到月份
var date = time.getDate(); //得到日期
return new Date(year, month, date);
}