What does the math.floor () function JS

0

Someone can explain to me the following code, especially the part of the operation with Math.floor () and why it is divided dif / (1000 * 24 * 60 * 60)

var fecha1 = "12/06/2008";
var dia1 = fecha1.substr(0, 2);
var mes1 = fecha1.substr(3, 2);
var anyo1 = fecha1.substr(6);

var fecha2 = "29/06/2008";
var dia2 = fecha2.substr(0, 2);
var mes2 = fecha2.substr(3, 2);
var anyo2 = fecha2.substr(6);

var nuevafecha1 = new Date(anyo1 + "," + mes1 + "," + dia1);
var nuevafecha2 = new Date(anyo2 + "," + mes2 + "," + dia2);

var Dif = nuevafecha2.getTime() - nuevafecha1.getTime();
var dias = Math.floor(Dif / (1000 * 24 * 60 * 60));
alert(dias);
    
asked by Ivxn 24.08.2016 в 18:27
source

2 answers

3

Here my two cents with a quick explanation:

// definir variables
var fecha1 = "12/06/2008";

// tomar los dos primeros caracteres de fecha1
var dia1 = fecha1.substr(0, 2);

// a partir de la posicion 3 de fecha1 tomar los 2 siguientes caracteres
var mes1 = fecha1.substr(3, 2);

// tomar todos los caracteres de fecha1 a partir de la posición 6
var anyo1 = fecha1.substr(6);

// igual que las anteriores
var fecha2 = "29/06/2008";
var dia2 = fecha2.substr(0, 2);
var mes2 = fecha2.substr(3, 2);
var anyo2 = fecha2.substr(6);

// crear una instancia de Date (fecha) con los valores obtenidos arriba
var nuevafecha1 = new Date(anyo1 + "," + mes1 + "," + dia1);

// crear otra instancia de Date
var nuevafecha2 = new Date(anyo2 + "," + mes2 + "," + dia2);

// obtener el valor numérico que corresponde a cada una de las fechas
// y obtener la diferencia (resta) entre la segunda y la primera.
// Este valor está dado en milisegundos
var Dif = nuevafecha2.getTime() - nuevafecha1.getTime();

// dividir el valor obtenido arriba entre la constante equivalente a 1 día
// Redondear el resultado de la división al entero menor con la función Math.floor()
// de forma tal que si el resultado es 17.3432, se obtendrá 17
var dias = Math.floor(Dif / (1000 * 24 * 60 * 60));

// mostrar una ventana emergente con el resultado final
alert(dias);

Going into detail in why it is divided by 1000 * 24 * 60 * 60, comes from the fact that the value obtained from getTime() is given in milliseconds, then to obtain the equivalent in days, it is necessary to arm the equivalent to one day:

  

1000 milliseconds = 1 second

     

x 60 seconds = 1 minute

     

x 60 minutes = 1 hour

     

x 24 hours = 1 day

    
answered by 24.08.2016 / 18:42
source
0

I slightly optimized your code and the answer of why this mathematical operation is to get the day out but I do not see it necessary in this case because this operation will not change, it will always be the same to obtain 1:

function cambiarFormato(fecha){
 var fechaSplit = fecha.split('/')
 return new Date(fechaSplit[2],fechaSplit[1], fechaSplit[0])
}

var fecha1 = "12/06/2008"
var nuevafecha1 = cambiarFormato(fecha1);

var fecha2 = "29/06/2008";
var nuevafecha2 = cambiarFormato(fecha2)
var Dif = nuevafecha2.getTime() - nuevafecha1.getTime();
var dias = Math.floor(Dif / (1000 * 24 * 60 * 60));
alert(dias);
    
answered by 24.08.2016 в 18:53