根据日期范围获取日期区间内的月份数组或日期数组
作者:互联网
/**
* 功能:传入开始时间,结束时间及事件类型返回范围内的月份数组或者日期数组
* @param startDateTime
* @param endDateTime
* @param dateType
* @returns {*[]}
*/
export function getDateArry(startDateTime, endDateTime, dateType) {
if (dateType === 'month') {
return getMonth(startDateTime, endDateTime);
} else if (dateType === 'day') {
return getDay(startDateTime, endDateTime);
}
}
/**
* 获取日期范围内天的数组
* @param startDate
* @param endDate
* @returns {any[]}
*/
function getDay(startDate, endDate) {
var result = new Array();
var ab = startDate.split("-");
var ae = endDate.split("-");
var db = new Date();
db.setUTCFullYear(ab[0], ab[1] - 1, ab[2]);
var de = new Date();
de.setUTCFullYear(ae[0], ae[1] - 1, ae[2]);
var unixDb = db.getTime();
var unixDe = de.getTime();
for (var k = unixDb; k <= unixDe; ) {
result.push(formatDate(new Date(parseInt(k)), "yyyy-MM-dd"));
k = k + 24 * 60 * 60 * 1000;
}
console.log(result);
return result;
}
/**
* 获取日期范围内月份数组
* @param start
* @param end
* @returns {*[]}
*/
function getMonth(start, end) {
var result = [];
var s = start.split("-");
var e = end.split("-");
var min = new Date();
var max = new Date();
min.setFullYear(s[0], s[1] * 1 - 1, 1); //开始日期
max.setFullYear(e[0], e[1] * 1 - 1, 1); //结束日期
var curr = min;
while (curr <= max) {
var month = curr.getMonth();
result.push(formatDate(curr, "yyyy-MM"));
curr.setMonth(month + 1);
}
return result;
}
/**
* 格式化
* @param date
* @param fmt
* @returns {string|null}
*/
function formatDate(date, fmt) {
if (date === "" || date === null || date === undefined) {
return null;
}
if (fmt === "" || fmt === null || fmt === undefined) {
fmt = "yyyy-MM";
}
date = new Date(date);
var o = {
"M+": date.getMonth() + 1, // 月份
"d+": date.getDate(), // 日
"h+": date.getHours(), // 小时
"m+": date.getMinutes(), // 分
"s+": date.getSeconds(), // 秒
"q+": Math.floor((date.getMonth() + 3) / 3), // 季度
S: date.getMilliseconds(), // 毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(
RegExp.$1,
(date.getFullYear() + "").substr(4 - RegExp.$1.length)
);
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
);
}
return fmt;
}
/******************************************************/
/**
* 获取周,目前用不上 先留着吧
* @param start
* @param end
* @returns {*[]}
*/
function getWeak(start, end) {
// 1、获取开始日期属于第几周,哪年
let startWeekNum = getWeekNum(start);
let startYearNum = parseInt(start.split("-")[0]);
if (startWeekNum >= 53) {
startWeekNum = 52;
}
// 2、获取结束日期属于第几周,哪年
let endWeekNum = getWeekNum(end);
let endYearNum = parseInt(end.split("-")[0]);
if (endWeekNum >= 53) {
endWeekNum = 52;
}
// 3、计算周数
let result = [];
let yearAdd = 0; // 跨年数
let yearWeekNum = startWeekNum; // 当前取到的周数
for (
let i = 0;
i <= (endYearNum - startYearNum) * 52 - startWeekNum + endWeekNum;
i++
) {
if (startWeekNum + i <= 0) {
// 如果开始周数是0,则取开始年上一年的52周
result.push(startYearNum - 1 + "52");
} else {
if (yearWeekNum === 52) {
yearAdd++;
}
yearWeekNum = startWeekNum + i - yearAdd * 52;
if (yearWeekNum <= 0) {
// 如果开始周数是0,则取开始年上一年的52周
result.push(startYearNum + "52");
} else {
if (yearWeekNum < 10) {
result.push(startYearNum + yearAdd + "0" + yearWeekNum);
} else {
result.push(startYearNum + yearAdd + "" + yearWeekNum);
}
}
}
}
return result;
}
function getWeekNum(dt) {
let d1 = new Date(dt);
let d2 = new Date(dt);
d2.setMonth(0);
d2.setDate(1);
let rq = d1 - d2;
let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
let num = Math.ceil(days / 7);
return num;
}
标签:ab,ae,endDateTime,param,获取,日期,let,数组,var 来源: https://www.cnblogs.com/zy666-blog/p/16351514.html