JS獲取當前時間(YYYY-MM-DD ),element顯示默認當前時間,顯示默認昨天,顯示默認上個月

原文連接:點我html

進來的隨便看看,或許有幫助vue

vue+element-ui   datepicker 設置默認日期
用的框架是vue+element-ui ,如下是時間控件element-ui

1 <el-form-item label="月份">
2     <el-date-picker v-model="ct_month" type="month" placeholder="選擇月份" format="yyyy 年 MM 月 " value-format="yyyy-MM">
3 </el-date-picker> 4 </el-form-item>

 


因爲我須要顯示的是默認月份而不是具體時間日期,大家須要的能夠換成框架

type="date"ui

format="yyyy 年 MM 月dd日 "this

具體設置請移步 :連接spa

 1 設置默認屬性  2 ct_month: null,  3 方法:  4  
 5 
 6 getdatatime(){//默認顯示今天
 7         this.ct_month= new Date();  8       
 9 }, 10  
11  
12 getdatatime(){//默認顯示昨天
13         this.ct_month= new Date(); 14         this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24); 15 }, 16  
17 getdatatime(){//默認顯示上週
18         this.ct_month= new Date(); 19         this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24 * 7); 20 }, 21  
22 getdatatime(){//默認顯示上個月
23         this.ct_month= new Date(); 24         this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24 * 30); 25 }, 26

把方法放在全局裏面,也就是說一跳到這個頁面就執行這個方法.net


注意:個人是顯示月份,不是具體的日期,轉具體日期下面有寫code

如下方法是JS獲取當前時間格式爲YYYY-MM-DDcomponent

把註釋的去掉就是YYYY-MM-DD HH:SS

 1 getdatatime() {  2                 this.ct_month= new Date();  3                 this.ct_month.setTime(this.ct_month.getTime() - 3600 * 1000 * 24 * 30);//獲取上個月的日期(這一行去掉就是獲取今天計算機上的日期了)
 4                 
 5                 var now = this.ct_month;  6  
 7                 var year = now.getFullYear(); //
 8                 var month = now.getMonth() + 1; //
 9                 var day = now.getDate(); //
10  
11 // var hh = now.getHours(); //時
12 // var mm = now.getMinutes(); //分
13  
14                 var clock = year + "-"; 15  
16                 if(month < 10) 17                     clock += "0"; 18  
19                 clock += month + "-"; 20  
21                 if(day < 10) 22                     clock += "0"; 23  
24                 clock += day + " "; 25  
26 // if(hh < 10)
27 // clock += "0";
28 // 29 // clock += hh + ":";
30 // if(mm < 10) clock += '0';
31 // clock += mm;
32                 
33  console.log(clock); 34             },

 

 

分界線

datepicker 設置默認日期
 

1 //今天$('#reportrange span').html(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss') + ' - ' +moment().endOf('day').format('YYYY-MM-DD HH:mm:ss'));

2 //昨天$('#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'));

3 //過去七天$('#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'));
4 //默認30天$('#reportrange span').html(moment().subtract(29, 'days').format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().format('YYYY-MM-DD HH:mm:ss'));

5 //默認這個月$('#reportrange span').html(moment().startOf('month').format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment().endOf('month').format('YYYY-MM-DD HH:mm:ss'));

6 //默認上個月$('#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'));
相關文章
相關標籤/搜索