All files / src/core formatearfecha.ts

100% Statements 53/53
100% Branches 45/45
100% Functions 1/1
100% Lines 53/53

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961x 1x 1x               1x 234x   234x   230x 230x     152x 2x   150x 2x   148x 2x   146x 2x   144x 143x   1x     20x 1x   19x 1x   18x 1x   17x 15x   2x 1x   1x     25x 1x   24x 1x   23x 1x   22x 20x   2x 1x   1x     33x 1x   32x 1x   31x 1x   30x 28x   2x 1x   1x       4x      
import { getFormatoFecha } from "./getformatofecha";
import { CodigoFormatoFecha } from "../constants";
import { disgregarFecha } from "./disgregarfecha";
 
/**
 * 
 * @param fecha 
 * @param tipoFormato
 * Asigna alguno de los formatos validos a una fecha dada. 
 */
export function setFormatoFecha( fecha: string, tipoFormato: CodigoFormatoFecha ) : string {
    try {
 
        const codigoFormatoFecha = getFormatoFecha(fecha);
 
        const {dia, mes, anio} = disgregarFecha(fecha);
        switch ( codigoFormatoFecha ){
            case CodigoFormatoFecha.FMT_ESPANOL: case CodigoFormatoFecha.FMT_ITALIANO:
            /* dd/mm/yyyy o dd-mm-yyyy*/
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ITALIANO){
                    return fecha.replace(/[/]/g, '-');
                }
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ESPANOL){
                    return fecha.replace(/[-]/g, '/');
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_PLANO){
                    return fecha.replace(/[/-]/g,'');
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSOPLANO){
                    return anio + mes + dia;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSO){
                    return anio + '/' + mes  + '/' + dia;
                }
                return fecha;
            case CodigoFormatoFecha.FMT_PLANO:
            /* ddmmyyyy*/
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ITALIANO){
                    return dia + '-' + mes  + '-' + anio;
                }
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ESPANOL){
                    return dia + '/' + mes  + '/' + anio;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSOPLANO){
                    return anio + mes + dia;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSO){
                    return anio + '/' + mes  + '/' + dia;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_PLANO){
                    return fecha;
                }
                return fecha;
            case CodigoFormatoFecha.FMT_INVERSOPLANO:
            /* yyyymmdd */
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ITALIANO){
                    return dia + '-' + mes + '-' + anio;
                }
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ESPANOL){
                    return dia + '/' + mes + '/' + anio;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_PLANO){
                    return dia + mes + anio;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSO){
                    return anio + '/' + mes + '/' + dia;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSOPLANO){
                    return fecha;
                }
                return fecha;
            case CodigoFormatoFecha.FMT_INVERSO:
            /* yyyy/mm/dd */
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ITALIANO){
                    return dia + '-' + mes + '-' + anio;
                }
                if (tipoFormato ===  CodigoFormatoFecha.FMT_ESPANOL){
                    return dia + '/' + mes + '/' + anio;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_PLANO){
                    return dia + mes + anio;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSO){
                    return fecha;
                }
                if (tipoFormato === CodigoFormatoFecha.FMT_INVERSOPLANO){
                    return anio + mes + dia;;
                }
                return fecha;
        }
 
    } catch (error) {
        throw (error);
    }
 
}