1.template中使用:
<el-form-item>
<date-range-picker
v-model="deviceFormData.time"
class="date-item"
:start-placeholder="$t('NeoLight.startTime')"
:end-placeholder="$t('NeoLight.endTime')"
value-format="yyyy-MM-dd"
:picker-options="pickerOptions"
/>
</el-form-item>
2.data中定义:
data() {
return {
deviceFormData: {
time: [],
},
//不能选择当前日期之后的时间
pickerOptions: {
disabledDate(time) {
return time.getTime() > Date.now();
}
},
}
};
},
3.methods方法:
created() {
this.getTimeFn();
},
// 设置默认时间
getTimeFn() {
const that = this;
const end = new Date();
const start = new Date();
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
this.deviceFormData.time[0] = that.formatDate(start);
this.deviceFormData.time[1] = that.formatDate(end);
},
// 格式化时间
formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return myyear + "-" + mymonth + "-" + myweekday;
},
4.实现效果:
小tips 时 分 秒 格式化时间
/**
* 格式化时间 到 时 分 秒
*/
formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
var h = date.getHours();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
h = h < 10 ? "0" + h : h;
var m = date.getMinutes();
m = m < 10 ? "0" + m : m;
var s = date.getSeconds();
s = s < 10 ? "0" + s : s;
return (
myyear + "-" + mymonth + "-" + myweekday + " " + h + ":" + m + ":" + s
);
},
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/79291.html