js small case - to calculate the time difference

Small Case - calculate the time difference
 
Clicks on the button, the time difference between the two is calculated, the date is converted mainly milliseconds, there are three methods, Date.parse used here, this is recommended
 
* Subtraction time and time: the correlation calculation time, we have to turn to a time epoch (time to turn into a few milliseconds), and then subtracting the result then converted into the specified format
* GetTime () Gets era time
* Date.parse ( "2015-08-24") // returns the number of milliseconds away from the specified date 1970-1-1 zero
* Date.now (); 1970-1-1 number of milliseconds from when the zero return line executes //
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <input type="date" id="num"><input type="date" id="num1">
    <input type="submit" value="时间差" id="btn">
    <span id="res"></span>
</body>
<script>
    (function () {
        var num = document.getElementById('num');
        var num1 = document.getElementById('num1');
        var BTN = document.getElementById ( 'BTN' );
         var RES = document.getElementById ( 'RES' ); 
        btn.onclick = function () {
             var num2 = num.value; // acquired in the form of date 2019-12- 08 
            var num3 = num1.value;
             var StartNum = Date.parse (num2); // date into milliseconds 
            var endnum = Date.parse (num3);
             var TDOA = (endnum - StartNum) / 1000/60/60 / 24; // subtract the number of days and then converted to 
            res.innerHTML = num2 + 'to' + num3 + 'difference' + tdoa + 'day' ; 

        } 
    }) ();
</script>

</html>

 

Guess you like

Origin www.cnblogs.com/muyun123/p/11424617.html