UNPKG

@victorequena22/utiles

Version:

Utilidades para mi uso que pongo a dispocion

84 lines (83 loc) 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseDia = exports.getSegundos = exports.diaToNumber = exports.fechaToNumber = exports.solicitud = exports.formatoHora = exports.formatoFecha = exports.timeFormatAtHora = exports.timeFormat = exports.numberFormat = exports.zfill = void 0; const dayjs = require("dayjs"); function zfill(numero, width = 6) { const numberOutput = Math.abs(numero); /* Valor absoluto del número */ const length = numero.toString().length; /* Largo del número */ if (width <= length) { if (numero < 0) { return ("-" + numberOutput.toString()); } else { return numberOutput.toString(); } } else { const complete = () => { let rest = ""; for (let i = length; i < width; i++) { rest += "0"; } return rest; }; if (numero < 0) { return ("-" + (complete()) + numberOutput.toString()); } else { return ((complete()) + numberOutput.toString()); } } } exports.zfill = zfill; function numberFormat(numero, decimal = 2) { return (numero .toFixed(decimal) .replace(".", ",") .replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.")); } exports.numberFormat = numberFormat; function timeFormat(s2, decimal = 2) { const s = Math.floor(s2); const m = Math.floor(s / 60); return `${zfill(m, 2)}:${zfill(s - (m * 60), 2)}${(s2 - s).toFixed(decimal).replace("0", "")}`; } exports.timeFormat = timeFormat; function timeFormatAtHora(s2, decimal = 2) { const s = Math.floor(s2); const m = Math.floor(s / 60); const h = Math.floor(m / 60); return `${zfill(h, 2)}:${zfill(m - (h * 60), 2)}:${zfill(s - (m * 60), 2)}${(s2 - s).toFixed(decimal).replace("0", "")}`; } exports.timeFormatAtHora = timeFormatAtHora; function formatoFecha(fecha) { return dayjs(fecha).format('DD/MM/YYYY'); } exports.formatoFecha = formatoFecha; function formatoHora(fecha) { return dayjs(fecha).format('hh:mm a'); } exports.formatoHora = formatoHora; function solicitud() { return dayjs().format('YYYY-MM-DD/HH:mm:ss'); } exports.solicitud = solicitud; function fechaToNumber(fecha) { return parseInt(dayjs(fecha).format('YYYYMMDDHHmmss')); } exports.fechaToNumber = fechaToNumber; function diaToNumber(fecha) { return parseInt(dayjs(fecha).format('YYYYMMDD')); } exports.diaToNumber = diaToNumber; function getSegundos(fecha) { const hora = parseInt(dayjs(fecha).format('HH')); const minuto = parseInt(dayjs(fecha).format('mm')); const segundo = parseInt(dayjs(fecha).format('ss')); return hora * 3600 + minuto * 60 + segundo; } exports.getSegundos = getSegundos; function parseDia(fecha) { return dayjs(fecha).format('DD-MM-YYYY'); } exports.parseDia = parseDia;