search

Home  >  Q&A  >  body text

javascript - js How to calculate the number of days, hours, minutes and seconds between two timestamps

js How to calculate the number of days, hours, minutes and seconds between two timestamps

仅有的幸福仅有的幸福2742 days ago1287

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:14:14

    <script>
        //当前时间与2017-04-04的时间差
        var startTime = "2017-04-04";
        var s1 = new Date(startTime.replace(/-/g, "/")),
        s2 = new Date(),
        runTime = parseInt((s2.getTime() - s1.getTime()) / 1000);
        var year = Math.floor(runTime / 86400 / 365);
        runTime = runTime % (86400 * 365);
        var month = Math.floor(runTime / 86400 / 30);
        runTime = runTime % (86400 * 30);
        var day = Math.floor(runTime / 86400);
        runTime = runTime % 86400;
        var hour = Math.floor(runTime / 3600);
        runTime = runTime % 3600;
        var minute = Math.floor(runTime / 60);
        runTime = runTime % 60;
        var second = runTime;
        console.log(year,month,day,hour,minute,second);
    </script>

    This is what I wrote before,

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-19 10:14:14

    var start=1491789600000;//2017-4-10 10:00
    var end=1494381600000;//2017-5-10 10:00
    
    var utc=end-start;
    
    utc/(24*60*60*1000);//30
    
    同样小时:utc/(60*60*1000);//720小时;
    
    分:utc/(60*1000);

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-19 10:14:14

    It is recommended to use third-party momentjs directly. If you think the answer is right, remember to follow fundebug (free full-stack monitoring website)

    reply
    0
  • Cancelreply