// 近一周
export function getOneWeek() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;//0-11表示1-12月
var day = now.getDate();
var dateObj = {};
dateObj.now = year + '-' + month + '-' + day;
if (day - 7 <= 0) { //如果在当月7日之前
var lastMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate(); //1周前所在月的总天数
if (month - 1 <= 0) { //如果在当年的1月份
dateObj.last = (year - 1) + '-' + 12 + '-' + (31 - (7 - day));
} else {
dateObj.last = year + '-' + (month - 1) + '-' + (lastMonthDay - (7 - day));
}
} else {
dateObj.last = year + '-' + month + '-' + (day - 7);
}
return dateObj;
}
// 近一个月
export function getOneMonth() {
var end = new Date();
var year = end.getFullYear();
var month = end.getMonth() + 1 < 10 ? `0${end.getMonth() + 1}` : end.getMonth() + 1
var day = end.getDate() > 9 ? end.getDate() : '0' + end.getDate();
var dateObj = {};
dateObj.now = year + '-' + month + '-' + day;
var endMonthDay = new Date(year, month, 0).getDate();
if (month - 1 <= 0) {
dateObj.last = (year - 1) + '-' + 12 + '-' + day;
} else {
var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();
if (startMonthDay < day) {
if (day < endMonthDay) {
dateObj.last = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day));
} else {
dateObj.last = year + '-' + (month - 1) + '-' + startMonthDay;
}
} else {
dateObj.last = year + '-' + ((month - 1) < 10 ? `0${(month - 1)}` : (month - 1)) + '-' + day;
}
}
return dateObj
}
// 近三个月
export function getThreeMounth() {
var end = new Date();
var year = end.getFullYear();
var month = end.getMonth() + 1 < 10 ? `0${end.getMonth() + 1}` : end.getMonth() + 1
var day = end.getDate() > 9 ? end.getDate() : '0' + end.getDate();
var dateObj = {};
dateObj.now = year + '-' + month + '-' + day;
var endMonthDay = new Date(year, month, 0).getDate(); //当前月的总天数
if (month - 3 <= 0) { //如果是1、2、3月,年数往前推一年
var start3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate(); //3个月前所在月的总天数
if (start3MonthDay < day) { //3个月前所在月的总天数小于现在的天日期
dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + start3MonthDay;
} else {
dateObj.last = (year - 1) + '-' + (12 - (3 - month)) + '-' + day;
}
} else {
var start3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate(); //3个月前所在月的总天数
if (start3MonthDay < day) { //3个月前所在月的总天数小于现在的天日期
if (day < endMonthDay) { //当前天日期小于当前月总天数,2月份比较特殊的月份
dateObj.last = year + '-' + (month - 3) + '-' + (start3MonthDay - (endMonthDay - day));
} else {
dateObj.last = year + '-' + (month - 3) + '-' + start3MonthDay;
}
} else {
dateObj.last = year + '-' + ((month - 3) < 10 ? `0${(month - 3)}` : (month - 3)) + '-' + day;
}
}
return dateObj
}
// 近半年
export function getHalfYear() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;//0-11表示1-12月
var day = now.getDate();
var dateObj = {};
dateObj.now = year + '-' + month + '-' + day;
// 先获取当前时间
var curDate = (new Date()).getTime()
// 将半年的时间单位换算成毫秒
var halfYear = 365 / 2 * 24 * 3600 * 1000
// 半年前的时间(毫秒单位)
var pastResult = curDate - halfYear
// 日期函数,定义起点为半年前
var pastDate = new Date(pastResult)
var pastYear = pastDate.getFullYear()
var pastMonth = pastDate.getMonth() + 1
var pastDay = pastDate.getDate()
if (pastMonth >= 1 && pastMonth <= 9) {
pastMonth = '0' + pastMonth
}
if (pastDay >= 0 && pastDay <= 9) {
pastDay = '0' + pastDay
}
dateObj.last = pastYear + '-' + pastMonth + '-' + pastDay
return dateObj
}
// 近一年
export function getOneYear() {
var nowDate = new Date()
var dates = new Date(nowDate)
var year = dates.getFullYear();
var month = dates.getMonth() + 1;//0-11表示1-12月
var day = dates.getDate();
var dateObj = {};
dateObj.now = year + '-' + month + '-' + day;
dates.setDate(dates.getDate() - 365)
var seperator1 = '-'
var year = dates.getFullYear()
var month = dates.getMonth() + 1
var strDate = dates.getDate()
if (month >= 1 && month <= 9) {
month = '0' + month
}
if (strDate >= 0 && strDate <= 9) {
strDate = '0' + strDate
}
dateObj.last = year + seperator1 + month + seperator1 + strDate
return dateObj
}