How to calculate date difference in JavaScript?

前端 未结 18 2105
执笔经年
执笔经年 2020-11-22 03:41

I want to calculate date difference in days, hours, minutes, seconds, milliseconds, nanoseconds. How can I do it?

18条回答
  •  梦谈多话
    2020-11-22 04:05

    function daysInMonth (month, year) {
        return new Date(year, month, 0).getDate();
    }
    function getduration(){
    
    let A= document.getElementById("date1_id").value
    let B= document.getElementById("date2_id").value
    
    let C=Number(A.substring(3,5))
    let D=Number(B.substring(3,5))
    let dif=D-C
    let arr=[];
    let sum=0;
    for (let i=0;i0||Number(B.substring(0,2)) - Number(A.substring(0,2))<0){
    days=Number(B.substring(0,2)) - Number(A.substring(0,2)) + sum_alter
    }
    
    if ((Number(B.substring(3,5)) == Number(A.substring(3,5)))){
    console.log(Number(B.substring(0,2)) - Number(A.substring(0,2)) + sum_alter)
    }
    
    time_1=[]; time_2=[]; let hour=[];
     time_1=document.getElementById("time1_id").value
     time_2=document.getElementById("time2_id").value
      if (time_1.substring(0,2)=="12"){
         time_1="00:00:00 PM"
      }
    if (time_1.substring(9,11)==time_2.substring(9,11)){
    hour=Math.abs(Number(time_2.substring(0,2)) - Number(time_1.substring(0,2)))
    }
    if (time_1.substring(9,11)!=time_2.substring(9,11)){
    hour=Math.abs(Number(time_2.substring(0,2)) - Number(time_1.substring(0,2)))+12
    }
    let min=Math.abs(Number(time_1.substring(3,5))-Number(time_2.substring(3,5)))
    document.getElementById("duration_id").value=days +" days "+ hour+"  hour " + min+"  min " 
    }
    
    
    





提交回复
热议问题