All files / src/core restarfechas.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 1/1
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x 1x 1x 1x                 1x 28x   28x 3x     20x 14x   10x 10x   10x 10x   10x   10x   10x     18x      
import { getFormatoFecha } from "./getformatofecha";
import { setFormatoFecha } from "./formatearfecha";
import { CodigoFormatoFecha, ErroresComunes } from "../constants";
import { validarFecha } from "./validarfecha";
 
/**
 * 
 * @param fecha1 
 * @param fecha2 
 * 
 * Obtiene la diferencia en días de dos fechas validas con el mismo formato
 */
export function restarFechas ( fecha1: string, fecha2: string ): number {
    try {
 
        if (getFormatoFecha(fecha1) !== getFormatoFecha(fecha2)) { 
            throw new Error(ErroresComunes.ERR_FMTOS_DISPARES)
        }
 
        validarFecha(fecha1);
        validarFecha(fecha2);
 
        let aFecha1 = new Date(fecha1);
        let aFecha2 = new Date(fecha2);
 
        aFecha1 = new Date(setFormatoFecha(fecha1, CodigoFormatoFecha.FMT_INVERSO));
        aFecha2 = new Date(setFormatoFecha(fecha2, CodigoFormatoFecha.FMT_INVERSO));
                
        const diff = aFecha1.getTime() - aFecha2.getTime();
        
        const diffDays = Math.ceil(diff / (1000 * 3600 * 24));        
 
        return diffDays;
 
    } catch (err) {
        throw (err);
    }
 
}