Error counting the same day

1

I have a system that calculates the number of days but the problem is that the client wants that the current day I also add them. Example: if I choose today 12-13-2018 at 12-15-2018, it results in 2 days and I have to count 3 days.

script.js

function data(valor){
    let ingreso = document.getElementById("ingreso").value;
    let retiro = document.getElementById("retiro").value;
    let fechaInicio = new Date(ingreso).getTime();
    let fechaFin    = new Date(retiro).getTime();
    let diff = fechaFin - fechaInicio; //Diferencia en milisegundos
    let dias = diff/(1000*60*60*24); //Diferencia en dias
    document.getElementById("totaldias").value = dias;
    document.getElementById("valor").value = dias*valor;
    //document.getElementById("dolares").value = valor*tasa_cambio;
}

index.php

<?php
    $tarjeta = 200;
    $efectivo = $tarjeta*0.5;
?>

<input type="date" name="ingreso" id="ingreso" autocomplete="off">
<input type="date" name="salida" id="retiro" autocomplete="off" onChange="data(<?php echo $tarjeta;?>)">
<input type="text" name="dias" id="totaldias" readonly="readonly">
    
asked by Stn 13.12.2018 в 20:55
source

1 answer

2

In the end, your problem is reduced to adding an extra day to the difference between dates. You can do it (at least) in two ways.

Using the Date object

You can use the Date object, which handles the month and year steps well, using:

let fechaFin = new Date(retiro) + 1)

The problem is that this will result in a string . You can pass it to Date and then do the getTime:

let fechaFin = new Date(new Date(retiro) + 1)).getTime()

Using milliseconds

Another option is to add the milliseconds that you have one day directly to the difference:

let diff = (fechaFin - fechaInicio) + (24*60*60*1000);

    
answered by 13.12.2018 / 21:06
source