1.注意事项 必须new Date('2020-1-4')格式的,而且在data里面定义的时候不能定义在form表单中,直接定义在data下面就可以了
data里面定义
data() {
retrun{
onLineStartTime:this.timeData(new Date(dayjs().year()+ '-' +(dayjs().month()+1)+ '-' +dayjs().date())),//默认值,需要是new Date('2020-1-4'')格
式开始时间
onLineEndTime:this.time(dayjs().year()+ '-' +(dayjs().month()+1)+ '-' +dayjs().date()),//结束时间
}
}
methods里面的方法 处理推迟30天
methods: {
//月 日补0
timeData(){
var beforeDat = new Date;
var befMonth = beforeDat.getMonth() + 1;
var mon = befMonth >= 10 ? befMonth : '0' + befMonth;
var befDate = beforeDat.getDate();
var da = befDate >= 10 ? befDate : '0' + befDate;
var newDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
return newDate
},
//当前时间往后推30天
time(datt){
if(datt){
// var datt = '2018-07-07'.split('-');//这边给定一个特定时间
datt = datt.split('-');
var newDate = new Date(datt[0], datt[1] - 1, datt[2]);
var befminuts = newDate.getTime() + 1000 * 60 * 60 * 24 * parseInt('30');//计算前几天用减,计算后几天用加,最后一个就是多少天的数量 n为向前或者向后天数
var beforeDat = new Date;
beforeDat.setTime(befminuts);
var befMonth = beforeDat.getMonth() + 1;
var mon = befMonth >= 10 ? befMonth : '0' + befMonth;
var befDate = beforeDat.getDate();
var da = befDate >= 10 ? befDate : '0' + befDate;
var newDate = beforeDat.getFullYear() + '-' + mon + '-' + da;
return newDate
}
},
}
3.结构
<el-form-item label="上线时间" prop="onlineTime" v-if="(formNew.checkStatus==1) && this.isShowTime || (formNew.checkStatus==2) && (type === 'view')&&!this.isShowTime">
<div class="date-box">
<el-date-picker
:disabled="type == 'view'"
class="date-input"
v-model="onLineStartTime"
type="date"
value-format="yyyy-MM-dd"
format="yyyy-MM-dd"
:picker-options="pickerOptions"
placeholder="选择开始日期">
</el-date-picker>
<div class="data-tip">至</div>
<el-date-picker
:disabled="type == 'view'"
class="date-input"
v-model="onLineEndTime"
type="date"
value-format="yyyy-MM-dd"
format="yyyy-MM-dd"
:picker-options="pickerOptionsEnd"
placeholder="选择结束日期">
</el-date-picker>
</div>
</el-form-item>
4.详情默认值得时候
//时间赋值
if(!res.data.onlineTime){
this.onLineStartTime=this.timeData(new Date(dayjs().year()+ '-' +(dayjs().month()+1)+ '-' +dayjs().date()))//默认值,需要是new Date('2020-1-4'')格式开始时间
this.onLineEndTime=this.time(dayjs().year()+ '-' +(dayjs().month()+1)+ '-' +dayjs().date())//结束时间
}else{
this.onLineStartTime=res.data.onlineTime.split(",")[0]
this.onLineEndTime=res.data.onlineTime.split(",")[1]
}
this.formNew.onlineTime=this.onLineStartTime+','+this.onLineEndTime
年月日
getTodayDate() { //月 日补0
let nowDate = new Date()
let year = nowDate.getFullYear()
let month = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1
let day = nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate.getDate()
let dateStr = year + '-' + month + '-' + day
return dateStr
},
网友评论