最终格式如下:["2023-08-08 11:45:50", "2023-08-15 11:45:50"] 或 最终格式如下:["2023-08-08", "2023-08-15"]
/**
* @Event 方法
* @description: 获取最近num天的时间
* @param: num: 最近的天数, isDateTime: 是否需要时分秒(默认是)
* isDateTime 最终格式如下:["2023-08-08 11:45:50", "2023-08-15 11:45:50"]
* !isDateTime 最终格式如下:["2023-08-08", "2023-08-15"]
* */
export function getLastNumDay(num, isDateTime=true) {
const now = new Date();
const sevenDaysAgo = new Date(now.getTime() - num * 24 * 60 * 60 * 1000);
const endDate = formatDate(now);
const startDate = formatDate(sevenDaysAgo);
// console.log(`起始日期时间:${startDate},结束日期时间:${endDate}`);
function formatDate(date) {
const year = date.getFullYear();
const month = formatNumber(date.getMonth() + 1);
const day = formatNumber(date.getDate());
const hours = formatNumber(date.getHours());
const minutes = formatNumber(date.getMinutes());
const seconds = formatNumber(date.getSeconds());
return isDateTime ? `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` : `${year}-${month}-${day}`;
}
function formatNumber(num) {
return num > 9 ? num : `0${num}`;
}
return [startDate, endDate];
}
// 使用
import { getLastNumDay } from '@/utils/publicFun'
let arr = getLastNumDay(30, false)