js 計算兩個時間的差

來源:互聯網
上載者:User

標籤:style   ar   div   代碼   log   時間   line   new   

比如得到兩個時間的字串 2013-01-21 11:10:49   2013-01-21 11:14:43,要用後面的時間減去前面的時間

直接上代碼

var begen = new Date(value[0]["createtime"])var end = new Date(value[i]["createtime"])var cha = end.getTime()-begen.getTime();console.log(cha);console.log(value[0]["createtime"]);console.log(value[i]["createtime"]);  var leave1=cha%(24*3600*1000)    //計算天數後剩餘的毫秒數var hours=Math.floor(leave1/(3600*1000)) //相差的小時var leave2=leave1%(3600*1000)        //計算時數後剩餘的毫秒數var minutes=Math.floor(leave2/(60*1000))//相差的分鐘var leave3=leave2%(60*1000)      //計算分鐘數後剩餘的毫秒數var seconds=Math.round(leave3/1000)//相差的秒console.log(hours+"小時 "+minutes+"分鐘 "+seconds+"秒");






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.