js計算每個月某日某時的時間戳

通常項目中使用到提醒預定事件時,會設置某一個時間點事件活動開始時間 好比某活動每個月8號10點開始,獲取到該時間點的時間戳以下:ui

var date = new Date();
    var time;

    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();

    var activeDay, nextDate;
    // 獲取當月有多少天
    var totalDay = new Date(year, month, 0).getDate();

    if (day < 8) {
        activeDay = 8 - day;
        day = activeDay + day;
        nextDate = `${year}/${month }/${day}`

    } else if (day > 8) {
        if (totalDay == 31) {
            activeDay = 31 - day + 8;
            day = day + activeDay - totalDay;

        } else if (totalDay == 30) {
            activeDay = 30 - day + 8;
            day = day + activeDay - totalDay;
        } else if (totalDay == 29) {
            activeDay = 29 - day + 8;
            day = day + activeDay - totalDay;
        } else if (totalDay == 28) {
            activeDay = 28 - day + 8;
            day = day + activeDay - totalDay;
        }

        nextDate = `${year}/${month + 1}/${day}`

    } else if (day == 8) {
        // time = date.getTime();
        day = 8;
        nextDate = `${year}/${month}/${day}`
    }

    time = new Date(nextDate).getTime() + 10 * 60 * 60 * 1000;
    console.log(time);
    
複製代碼
相關文章
相關標籤/搜索