准备工作:后台返回的日期格式转换为new Date()形式
参数date是后台返回的日期格式是:2020-11-12
转换为中国标准时间也就是 new Date()形式的
通过以下函数实现:
var chulitime = function (date) {
var t = Date.parse(date)
if (!isNaN(t)) {
return new Date(Date.parse(date.replace(/-/g, '/')))
}
};
准备工作: 日期格式化
最终得到的近七日数据实际上是中国标准时间,需要将其转换为 2020-11-30形式的
//日期格式化
var dateFormat = function (date){ //date为中国标准时间:例 new Date()结果的格式
var year = date.getFullYear().toString();
var month = date.getMonth() + 1;
if(month<10) {
month = "0" + month.toString();
} else {
month = month.toString();
}
var day = date.getDate();
if(day <10) {
day = "0" + day .toString();
} else {
day = day .toString();
}
return year+'-'+month+'-'+day; //2020-11-30
}
准备工作:获取近七日的数据
//获取近七天日期
var getRencentlySevenDay = function (tarDay){ //tarDay为中国标准时间,也就是chulitime函数处理之后的数据
var tarDay1=dateFormat(new Date(tarDay.getTime()-(0*24*60*60*1000)));
var tarDay2=dateFormat(new Date(tarDay.getTime()-(1*24*60*60*1000)));
var tarDay3=dateFormat(new Date(tarDay.getTime()-(2*24*60*60*1000)));
var tarDay4=dateFormat(new Date(tarDay.getTime()-(3*24*60*60*1000)));
var tarDay5=dateFormat(new Date(tarDay.getTime()-(4*24*60*60*1000)));
var tarDay6=dateFormat(new Date(tarDay.getTime()-(5*24*60*60*1000)));
var tarDay7=dateFormat(new Date(tarDay.getTime()-(6*24*60*60*1000)));
return targetDay = [tarDay1,tarDay2,tarDay3,tarDay4,tarDay5,tarDay6,tarDay7]//return其实在内部是进行了一个操作,声明了一个变量的。
}
真实的运用:demo
console.log(this.gongdanList.dataCopy[0].rptTime) //2020-11-12 15:03:29
var time = chulitime(this.gongdanList.dataCopy[0].rptTime)
console.log(time) //Thu Nov 12 2020 15:03:29 GMT+0800 (中国标准时间)
var jinqiri = getRencentlySevenDay(time)
console.log(jinqiri)//["2020-11-12", "2020-11-11", "2020-11-10", "2020-11-09", "2020-11-08", "2020-11-07", "2020-11-06"]