JS gets the first and last day of the current month

directly on the code

/**
 * @description 获取当前月第一天 最后一天 当前天
 * @author xiahl
 * @html
 * @param  // type 0 第一天; 1 最后一天; 不传 当天;
 */
function getCurMonthFirstOrLast(type = 2) {
    
    
    let curT = new Date;
    let y = curT.getFullYear(); //获取年份
    let m = curT.getMonth() + 1; //获取月份
    let d = ['1', new Date(y, m, 0).getDate(), curT.getDate()][type];
    m = m < 10 ? "0" + m : m; //月份补 0
    d = d < 10 ? "0" + d : d; //日数补 0
    return [y, m, d].join("/");
};
    console.log(getCurMonthFirstOrLast(0)); // 2023/02/01
    console.log(getCurMonthFirstOrLast(1)); // 2023/02/28
    console.log(getCurMonthFirstOrLast()); // 2023/02/13

insert image description here

The default separator here is "/ ", which can be adjusted according to the business

Guess you like

Origin blog.csdn.net/qq_45284938/article/details/129005137