fenzhi-utils
Version:
分值前端项目的js函数库
29 lines (28 loc) • 886 B
JavaScript
/**
* 获取时间所在月份起止日期
* @param {date} params 时间,不传默认当前
* @returns {array}
*/
/**
CustomMonthRate(); // ['2023-3-01', '2023-3-31']
CustomMonthRate('2023-05-01'); // ['2023-5-01', '2023-5-31']
CustomMonthRate('2023-05-31'); // ['2023-5-01', '2023-5-31']
CustomMonthRate(new Date()); // ['2023-3-01', '2023-3-31']
CustomMonthRate('2023-02-21'); // ['2023-2-01', '2023-2-28']
*/
export function CustomMonthRate(params) {
let date;
if (!arguments.length) {
date = new Date();
} else {
if ([null, '', undefined].includes(params)) {
return ['', ''];
} else {
date = new Date(params);
}
}
const year = date.getFullYear();
const month = date.getMonth() + 1;
const lastDate = new Date(year, month, 0).getDate();
return [`${year}-${month}-01`, `${year}-${month}-${lastDate}`];
}