JS獲取當前時間(YYYY-MM-DD ),element顯示預設當前時間,顯示預設昨天,顯示預設上個月,
阿新 • • 發佈:2019-02-05
進來的隨便看看,或許有幫助
vue+element-ui datepicker 設定預設日期
用的框架是vue+element-ui ,以下是時間控制元件
<el-form-item label="月份">
<el-date-picker v-model="ct_month" type="month" placeholder="選擇月份" format="yyyy 年 MM 月 " value-format="yyyy-MM"></el-date-picker>
</el-form-item>
由於我需要顯示的是預設月份而不是具體時間日期,你們需要的可以換成
type="date"
format="yyyy 年 MM 月dd日 "
具體設定請移步
設定預設屬性 ct_month: null, 方法: getdatatime(){//預設顯示今天 this.ct_month= new Date(); }, getdatatime(){//預設顯示昨天 this.ct_month= new Date(); this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24); }, getdatatime(){//預設顯示上週 this.ct_month= new Date(); this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24 * 7); }, getdatatime(){//預設顯示上個月 this.ct_month= new Date(); this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24 * 30); }, 把方法放在全局裡面,也就是說一跳到這個頁面就執行這個方法
主意。我的是顯示月份,不是具體的日期,轉具體日期下面有寫
以下方法是JS獲取當前時間格式為YYYY-MM-DD
把註釋的去掉就是YYYY-MM-DD HH:SS
getdatatime() { this.ct_month= new Date(); this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24 * 30);//獲取上個月的日期(這一行去掉就是獲取今天計算機上的日期了) var now = this.ct_month; var year = now.getFullYear(); //年 var month = now.getMonth() + 1; //月 var day = now.getDate(); //日 // var hh = now.getHours(); //時 // var mm = now.getMinutes(); //分 var clock = year + "-"; if(month < 10) clock += "0"; clock += month + "-"; if(day < 10) clock += "0"; clock += day + " "; // if(hh < 10) // clock += "0"; // // clock += hh + ":"; // if(mm < 10) clock += '0'; // clock += mm; console.log(clock); },
分界線
datepicker 設定預設日期
//今天$('#reportrange span').html(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss') + ' - ' +moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'));今天
//昨天$('#reportrange span').html(moment().subtract(1, 'days').startOf('day').format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().subtract(1, 'days').endOf('day').format('YYYY-MM-DD HH:mm:ss'));過去30天
//過去七天$('#reportrange span').html(moment().subtract(6, 'days').startOf('days').format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().endOf('days').format('YYYY-MM-DD HH:mm:ss'));過去30天
//預設30天$('#reportrange span').html(moment().subtract(29, 'days').format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().format('YYYY-MM-DD HH:mm:ss'));過去30天
//預設這個月$('#reportrange span').html(moment().startOf('month').format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().endOf('month').format('YYYY-MM-DD HH:mm:ss'));過去30天
//預設上個月$('#reportrange span').html(moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD HH:mm:ss'));