js兩個(gè)時(shí)間相減

在開發(fā)中,需要處理一些時(shí)間之間的差值,后端傳給我們開始時(shí)間與結(jié)束時(shí)間,我們則需要用當(dāng)前時(shí)間來判斷兩組時(shí)間之間的差值,在js中可以這樣寫,上代碼:

    let now = new Date()
    let year = now.getFullYear()
    let month = now.getMonth()
    let date = now.getDate()
    let hour = now.getHours()
    let minu = now.getMinutes();
    let sec = now.getSeconds()
    month = month + 1
    if (month < 10){ month = "0" + month;}
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    let time = year + '-' + month + '-' + date + ' ' + hour + ':' + minu + ':' + sec

    Date.prototype.diff = function(date){
        return (this.getTime() - date.getTime())/(24 * 60 * 60 * 1000)
     }

     let nowTime = new Date(now)
     let end = new Date('2021/03/26 12:43:45')
     let endTime = end.diff(nowTime)

如果想要向下取整可以這樣寫:

  let floorTime = Math.floor(endTime)

如果想要向上取整可以這樣寫:

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

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

  • 〇、前言 本文共108張圖,流量黨請慎重! 歷時(shí)1個(gè)半月,我把自己學(xué)習(xí)Python基礎(chǔ)知識(shí)的框架詳細(xì)梳理了一遍。 ...
    Raxxie閱讀 19,556評論 17 410
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 12,300評論 6 13
  • 第五章******************************************************...
    fastwe閱讀 794評論 0 0
  • 與 TensorFlow 的初次相遇 https://jorditorres.org/wp-content/upl...
    布客飛龍閱讀 4,012評論 2 89
  • 夜鶯2517閱讀 128,087評論 1 9

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