JS - 日期 - 獲取當前時間是本月第幾周

函數

function _getMonthWeek(theDate){
    let currentDay = new Date(theDate);
    console.info(currentDay); // 2019-05-07T07:36:00.883Z
    
    // 獲取該日期所在周的週六,如2019.5月的週六有4號、11號、18號、25號、31號
    let theSaturday = currentDay.getDate() + (6 - currentDay.getDay()); 
    console.info(theSaturday); // 11
    
    return Math.ceil(theSaturday / 7);
}

調用

let now = new Date();
console.info(now); // 2019-05-07T07:36:00.883Z
console.info(_getMonthWeek(now)); // 2 即2019年5月的第二週
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章