All files / src/core getformatofecha.ts

100% Statements 20/20
100% Branches 52/52
100% Functions 1/1
100% Lines 20/20

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 39 40 411x 1x             1x     1536x 1536x 1536x   1536x   748x 788x   359x     429x 156x     273x 97x     176x 176x 176x 176x 124x     52x    
import { CodigoFormatoFecha, ErroresComunes } from "../constants";
import { isNumero } from "./numeros";
 
/**
 * 
 * @param fecha 
 * Obtiene el formato de una fecha dada segĂșn los formatos definidos
 */
export function getFormatoFecha( fecha: string ): CodigoFormatoFecha {
    
    let dia, mes, anio;
    dia = fecha.substr(0, 2);
    mes = fecha.substr(2, 2);
    anio = fecha.substr(4, 4);
 
    if ( (fecha.substr(2, 1) === '/' && fecha.substr(5, 1) === '/') && fecha.length === 10 && (isNumero(fecha.split('/')[0]) && fecha.split('/')[0].length <= 2) && (isNumero(fecha.split('/')[1]) && fecha.split('/')[1].length <= 2) && (isNumero(fecha.split('/')[2]) && fecha.split('/')[2].length === 4) ) { 
        /*  dd/mm/yyyy  */
        return CodigoFormatoFecha.FMT_ESPANOL;
    } else if ( (fecha.substr(4, 1) === '/' && fecha.substr(7, 1) === '/') && (isNumero(fecha.split('/')[0]) && fecha.split('/')[0].length === 4) && (isNumero(fecha.split('/')[1]) && fecha.split('/')[1].length <= 2) && (isNumero(fecha.split('/')[2]) && fecha.split('/')[2].length <= 2) ) { 
        /*  yyyy/mm/dd  o fmto largo, ej: 2021-01-16T03:00:00.000Z */
        return CodigoFormatoFecha.FMT_INVERSO;
    }
    
    if ( (fecha.substr(2, 1) === '-' && fecha.substr(5, 1) === '-') && fecha.length <= 10 && (isNumero(fecha.split('-')[0]) && fecha.split('-')[0].length <= 2) && (isNumero(fecha.split('-')[1]) && fecha.split('-')[1].length <= 2) && (isNumero(fecha.split('-')[2]) && fecha.split('-')[2].length <= 4) ) { 
        return CodigoFormatoFecha.FMT_ITALIANO;
    }
 
    if ( isNumero(fecha) && fecha.length === 8 && (dia >= 1 && dia <= 31) && (mes >= 1 && mes <= 12) && (anio >= 1901 && anio <= 2150) ) { 
        return CodigoFormatoFecha.FMT_PLANO;
    }
 
    dia = fecha.substr(6, 2);
    mes = fecha.substr(4, 2);
    anio = fecha.substr(0, 4);
    if ( isNumero(fecha) && fecha.length === 8 && (dia >= 1 && dia <= 31) && (mes >= 1 && mes <= 12) && (anio >= 1901 && anio <= 2150)) { 
        return CodigoFormatoFecha.FMT_INVERSOPLANO; 
    }
 
    throw new Error(ErroresComunes.ERR_FECHA_INVALIDA)
 
}