js:计算两个日期相减之间的差值,获取对应的天、小时、分、秒

src\common\utils\dateFormat.js

/* 
s1, s2, date1, date2
dateCompute(typeStorage.curTime, new Date().getTime(), typeStorage.curDateString, new Date().toLocaleString());
*/
function dateCompute(s1, s2, date1, date2) {
    console.log("dateFormat.js~~~获取日期",{s1, s2, date1, date2})

    let newDate = {};
    // let s1 = date1.getTime(),
    // s2 = date2.getTime();
            
    let total = (s2 - s1)/1000;
    
        
    let day = parseInt(total / (24*60*60));//计算整数天数
    
    let afterDay = total - day*24*60*60;//取得算出天数后剩余的秒数
    
    let hour = parseInt(afterDay/(60*60));//计算整数小时数
    
    let afterHour = total - day*24*60*60 - hour*60*60;//取得算出小时数后剩余的秒数
    
    let min = parseInt(afterHour/60);//计算整数分
    
    let afterMin = total - day*24*60*60 - hour*60*60 - min*60;//取得算出分后剩余的秒数
    
    console.log("dateFormat.js~~~",{day, afterDay, hour, afterHour, min, afterMin});
    newDate = {
        day,
        afterDay,
        hour,
        afterHour,
        min,
        afterMin
    }

    return newDate;
}



/**
 * @namespace dateCompute
 * @type {Object}
 */
const t = {
    dateCompute
}


module.exports = t;


--------------------------------------------------------------------------------------------------------
index.vue

    import { dateCompute } from '@/common/utils/dateFormat.js';
    
    
    /* 此处的逻辑是入口页面调起微信订阅弹框后,点击允许/取消,需要1分钟后才重新调起订阅,typeStorage该字段存在缓存里,和下次进入页面的当前时间作比较
        let typeStorage = {
            route: _this.curPageRoute,
            type: type,
            temp_type: temp_type,
            templateIds: tamplt,
            curDate: new Date(), //记录当前对应本地时间 Fri Jun 10 2022 15:25:24 GMT+0800 (中国标准时间),存在缓存里变了curDate: "2022-06-10T07:56:15.146Z"
            curDateString: new Date().toLocaleString(), //记录当前对应本地时间 '2022/6/10 15:34:40'
            curTime: new Date().getTime() //记录当前时间戳 1654846039988
        };
     */
    
    //计算两个日期之间差值,
    let timediffObj = dateCompute(typeStorage.curTime, new Date().getTime(), typeStorage.curDateString, new Date().toLocaleString());
    console.log("timediffObj~~",{timediffObj})

    let timediff = timediffObj.min; //分钟
    console.log("计算是否过了1分钟",{timediff});//计算出分钟
    if(timediff >= 1) {
        console.log("已经过"+timediff+"分钟,重新调起订阅弹框")
        _this.getSubscribe();
    }else {
        console.log("还未超过"+timediff+"分钟条件不满足-直接跳转页面")
        
    }

参考原文链接:https://blog.csdn.net/qq_32584661/article/details/81632920?spm=1001.2101.3001.6650.6&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6-81632920-blog-124099642.pc_relevant_antiscanv4&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-6-81632920-blog-124099642.pc_relevant_antiscanv4&utm_relevant_index=10

你可能感兴趣的:(js:计算两个日期相减之间的差值,获取对应的天、小时、分、秒)