JS获取当前时间(YYYY-MM-DD ),element显示默认当前时间,显示默认昨天,显示默认上个月

原文链接:点我

进来的随便看看,或许有帮助

vue+element-ui   datepicker 设置默认日期
用的框架是vue+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"

format="yyyy 年 MM 月dd日 "

具体设置请移步 :链接

 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  

把方法放在全局里面,也就是说一跳到这个页面就执行这个方法

JS获取当前时间(YYYY-MM-DD ),element显示默认当前时间,显示默认昨天,显示默认上个月
注意:我的是显示月份,不是具体的日期,转具体日期下面有写

以下方法是JS获取当前时间格式为YYYY-MM-DD

把注释的去掉就是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'));