js:計算兩個日期相減之間的差值,獲取對應(yīng)的天、小時、分、秒

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));//計算整數(shù)天數(shù)
    
    let afterDay = total - day*24*60*60;//取得算出天數(shù)后剩余的秒數(shù)
    
    let hour = parseInt(afterDay/(60*60));//計算整數(shù)小時數(shù)
    
    let afterHour = total - day*24*60*60 - hour*60*60;//取得算出小時數(shù)后剩余的秒數(shù)
    
    let min = parseInt(afterHour/60);//計算整數(shù)分
    
    let afterMin = total - day*24*60*60 - hour*60*60 - min*60;//取得算出分后剩余的秒數(shù)
    
    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';
    
    
    /* 此處的邏輯是入口頁面調(diào)起微信訂閱彈框后,點擊允許/取消,需要1分鐘后才重新調(diào)起訂閱,typeStorage該字段存在緩存里,和下次進入頁面的當前時間作比較
        let typeStorage = {
            route: _this.curPageRoute,
            type: type,
            temp_type: temp_type,
            templateIds: tamplt,
            curDate: new Date(), //記錄當前對應(yīng)本地時間 Fri Jun 10 2022 15:25:24 GMT+0800 (中國標準時間),存在緩存里變了curDate: "2022-06-10T07:56:15.146Z"
            curDateString: new Date().toLocaleString(), //記錄當前對應(yīng)本地時間 '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("已經(jīng)過"+timediff+"分鐘,重新調(diào)起訂閱彈框")
        _this.getSubscribe();
    }else {
        console.log("還未超過"+timediff+"分鐘條件不滿足-直接跳轉(zhuǎn)頁面")
        
    }

參考原文鏈接: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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容