VUE获取上月、当月、前几月等等问题
【代码】VUE获取上月、当月、前几月等等问题。
·
很多时候 日期组件并不能完全满足需要,有时候页面需要内置一些按钮组件,比如
这个时候 就需要动态的更换传递的日期。
代码如下:
methods{
//时间解析
Time(now) {
let year=new Date(now).getFullYear();
let month=new Date(now).getMonth()+1;
let date=new Date(now).getDate();
if (month < 10) month = "0" + month;
if (date < 10) date = "0" + date;
return year+"-"+month+"-"+date
},
//获得某月的天数:
getMonthDays:function (myMonth){
let monthStartDate = new Date(new Date().getFullYear(), myMonth, 1);
let monthEndDate = new Date(new Date().getFullYear(), myMonth + 1, 1);
let days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
}
}
//用法: 比如 获取上月的数据(其余的情况 自己根据方法修改即可)
let startTime=this.Time(new Date(new Date().getFullYear(),new Date().getMonth()-1,1));
let endTime=this.Time(new Date(new Date().getFullYear(),new Date().getMonth()-1,this.getMonthDays(new Date().getMonth()-1)));
更多推荐
已为社区贡献4条内容
所有评论(0)