All files / src/core sumarmeses.ts

100% Statements 21/21
100% Branches 5/5
100% Functions 1/1
100% Lines 21/21

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 431x 1x 1x 1x 1x               1x 18x   18x   13x 13x   13x 13x   13x 13x 13x     9x   1x   1x   1x   1x     5x      
import { validarFecha } from "./validarfecha";
import { CodigoFormatoFecha } from "../constants";
import { setFormatoFecha } from "./formatearfecha";
import { getFormatoFecha } from "./getformatofecha";
import { disgregarFecha } from "./disgregarfecha";
 
/**
 * 
 * @param fecha 
 * @param nMeses
 * Suma N cantidad de meses a una determinada fecha sin importar el formato que este tenga, devolviendo la fecha en algunos de los formatos validos que ha sido enviado. Acepta 'sumar' nĂºmeros negativos.
 */
export function addMeses(fecha: string, nMeses: number): string {
    try {
 
        validarFecha(fecha)
        
        const dateConvert = setFormatoFecha(fecha, CodigoFormatoFecha.FMT_INVERSO);
        const aFecha = new Date(dateConvert);
    
        const newDate = new Date(aFecha.setMonth(aFecha.getMonth() + nMeses));
        const newDateString = newDate.toISOString().slice(0,10).replace(/[-]/g, '/');
    
        const {dia, mes, anio} = disgregarFecha(newDateString);    
        const fmto = getFormatoFecha(fecha);
        switch (fmto) 
        {
            case CodigoFormatoFecha.FMT_ESPANOL: 
                return dia + '/' + mes  + '/' + anio;
            case CodigoFormatoFecha.FMT_ITALIANO:
                return dia + '-' + mes  + '-' + anio;
            case CodigoFormatoFecha.FMT_PLANO:
                return dia + mes + anio;
            case CodigoFormatoFecha.FMT_INVERSOPLANO:
                return anio + mes  + dia;
            case CodigoFormatoFecha.FMT_INVERSO:
                return anio + '/' + mes  + '/' + dia;
        }
    } catch (error) {
        throw (error)
    }
    
}