Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

html - How can I convert long to int in JavaScript?

This HTML file calculate the difference in days between the current date and a selected date as long as it is later than today.
The final output shows how many days there are between the two dates, but I can't convert it from long to int.
Can anyone solve this problem?

here the codes:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Calcolo distanza</title>
    </head>
    <body onload="myFunction()">
    <p> Data corrente </p>
    <button type="button" id="myBtn"></button>
    <p> Scegli la data </p>
    <input type="date" id="dataCorrente">
    <input type="submit" onclick="calcola()">
    <br>
    <p id="Risultato"> </p>

    </body>
    <script>
        var d= new Date();
        function myFunction(){
            console.log("onload()");
            console.log(d);
            document.getElementById("myBtn").innerHTML=d.toDateString();
        }
        function calcola(){
            var input = document.getElementById("dataCorrente").value;
            if(input!=null){
                var dat=new Date();
                dat.setHours(0);
                dat.setMinutes(0);
                dat.setSeconds(0);
                var data = new Date(input); 
                var mill1=dat.getTime();
                var mill2=data.getTime();
                if(mill2>mill1){
                    mill2=mill2-mill1;
                    var ris=mill2/86400000;
                    document.getElementById("Risultato").innerHTML=ris;
                }
                else{
                    document.getElementById("Risultato").innerHTML="La data deve essere maggiore di quella corrente";
                }
            }
            else{
                console.log("input=null");
                document.getElementById("Risultato").innerHTML="Data non inserita";
            }
        }
    </script>
    </html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the parseInt() function like so:

var d = new Date();

function myFunction() {
  /*console.log("onload()");
  console.log(d);*/
  document.getElementById("myBtn").innerHTML = d.toDateString();
}

function calcola() {
  var input = document.getElementById("dataCorrente").value;
  if (input != null) {
    var dat = new Date();
    dat.setHours(0);
    dat.setMinutes(0);
    dat.setSeconds(0);
    var data = new Date(input);
    var mill1 = dat.getTime();
    var mill2 = data.getTime();
    if (mill2 > mill1) {
      mill2 = mill2 - mill1;
      var ris = mill2 / 86400000;
      document.getElementById("Risultato").innerHTML = parseInt(ris);
    } else {
      document.getElementById("Risultato").innerHTML = "La data deve essere maggiore di quella corrente";
    }
  } else {
    console.log("input=null");
    document.getElementById("Risultato").innerHTML = "Data non inserita";
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>Calcolo distanza</title>
</head>

<body onload="myFunction()">
  <p> Data corrente </p>
  <button type="button" id="myBtn"></button>
  <p> Scegli la data </p>
  <input type="date" id="dataCorrente">
  <input type="submit" onclick="calcola()">
  <br>
  <p id="Risultato"> </p>

</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...