[JS] Calculate the months between two months, and return all the months including themselves



/** 
 * 计算两个月之间的月份,返回包含他们自身所有的月份
 * @param { array } date 月份日期数组: [new Date(), new Date()] 或者 ["2023-02", "2023-06"]
 * @returns { array } 返回来两个月份之间所有的 月份 比如 ["2023-02", "2023-06"] 返回 ["2023-02", "2023-04", "2023-05", "2023-06"]
 */
const expandMonthsArray = (date) => {
    
    
    const startDate = new Date(date[0]);
    const endDate = new Date(date[1]);
    const result = [];
    while (startDate <= endDate) {
    
    
        result.push(formatDate(startDate, "yyyy-MM"));
        startDate.setMonth(startDate.getMonth() + 1);
    };
    return result;
};


About the formatDate function, please click here to view

Guess you like

Origin blog.csdn.net/weixin_44244230/article/details/132086956