el-date-picker中我想额外实现点击上一日按钮、下一日按钮显示对应的日期。这里面需要借助时间戳转换(先转换成时间戳,再讲时间戳转化成具体时间,然后截取日期)。
效果
代码
template:
<el-form :inline="true" :model="dataForm" @submit.native.prevent>
<el-form-item label="日期:">
<el-date-picker
v-model="selectDate2"
type="date"
size="small"
width="120"
placeholder="选择日期"
>
</el-date-picker>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" @click="goLast(1)"
>上一日</el-button
>
</el-form-item>
<el-form-item>
<el-button size="small" type="primary" @click="goNextDay(1)">下一日</el-button>
</el-form-item>
<el-form-item> (历史数据) </el-form-item>
</el-form>
js:
export default {
data() {
return {
selectDate: "",
selectDate2: ""
}
},
methods: {
//将10位或者13位时间戳转化为日期格式
timestampToTime(timestamp) {
if (String(timestamp).length == 10) {
var unixtimestamp = new Date(timestamp * 1000);
var year = 1900 + unixtimestamp.getYear();
var month = "0" + (unixtimestamp.getMonth() + 1);
var date = "0" + unixtimestamp.getDate();
var hour = "0" + unixtimestamp.getHours();
var minute = "0" + unixtimestamp.getMinutes();
var second = "0" + unixtimestamp.getSeconds();
return (
year +
"-" +
month.substring(month.length - 2, month.length) +
"-" +
date.substring(date.length - 2, date.length) +
" " +
hour.substring(hour.length - 2, hour.length) +
":" +
minute.substring(minute.length - 2, minute.length) +
":" +
second.substring(second.length - 2, second.length)
);
} else {
let currentTime = new Date(timestamp);
let year = currentTime.getFullYear(),
month = currentTime.getMonth() + 1,
day = currentTime.getDate(),
hour = currentTime.getHours(),
minute = currentTime.getMinutes(),
second = currentTime.getSeconds(),
millSeconds = currentTime.getMilliseconds();
let months = month < 10 ? "0" + month : month,
days = day < 10 ? "0" + day : day,
hours = hour < 10 ? "0" + hour : hour,
minutes = minute < 10 ? "0" + minute : minute,
seconds = second < 10 ? "0" + second : second,
millSecondss =
millSeconds < 10
? "00" + millSeconds
: millSeconds < 100
? "0" + millSeconds
: millSeconds;
return (
year +
"-" +
months +
"-" +
days +
" " +
hours +
":" +
minutes +
":" +
seconds +
"." +
millSecondss
);
}
},
//显示当前时间
getNowTime() {
let now = new Date(),
year = now.getFullYear(), //得到年份
month = now.getMonth(), //得到月份
date = now.getDate(); //得到日期
console.log(date);
let time = year + "-" + month + "-" + date;
this.selectDate = time;
this.selectDate2 = time;
},
//上一日
goLast(index) {
if (index === 0) {
let date1 = new Date(this.selectDate).getTime() - 3600 * 1000 * 24;
this.selectDate = this.timestampToTime(date1).substring(0,10);
} else if (index === 1) {
let date2 = new Date(this.selectDate2).getTime() - 3600 * 1000 * 24;
this.selectDate2 = this.timestampToTime(date2).substring(0,10);
}
},
//下一日
goNextDay(index) {
if (index === 0) {
let date3 = new Date(this.selectDate).getTime() + 3600 * 1000 * 24;
this.selectDate = this.timestampToTime(date3).substring(0,10);
} else if (index === 1) {
let date4 = new Date(this.selectDate2).getTime() + 3600 * 1000 * 24;
this.selectDate2 = this.timestampToTime(date4).substring(0,10);
}
}
},
created() {
this.getNowTime();
}
}
总结:
1.后面substring(0,10)是因为转换后的时间是精确到毫秒级的(13位时间戳)或者精确到秒级(10位时间戳)。