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);
    
复制代码
相关文章
相关标签/搜索