UNPKG

@genexus/web-standard-functions

Version:

GeneXus JavaScript standard functions library for web generators

181 lines 6.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TtoC = void 0; const isEmpty_1 = require("../date/isEmpty"); const padLeft_1 = require("../text/padLeft"); /** * Returns a string represantation of a date-time. dd[/]mm[/]yyyy HH:mm:ss * @return string */ const TtoC = (targetDate, dateDigit, hourDigit, dateFormat, timeFormat) => { if (!dateFormat) { dateFormat = "MDY"; } if (!timeFormat) { timeFormat = 12; } let day = (0, padLeft_1.padLeft)(String(targetDate.getDate()), 2, "0"); let month = (0, padLeft_1.padLeft)(String(targetDate.getMonth() + 1), 2, "0"); let year = targetDate.getFullYear().toString(); let hours = (0, padLeft_1.padLeft)(String(targetDate.getHours()), 2, "0"); let minutes = (0, padLeft_1.padLeft)(String(targetDate.getMinutes()), 2, "0"); let seconds = (0, padLeft_1.padLeft)(String(targetDate.getSeconds()), 2, "0"); let milliseconds = (0, padLeft_1.padLeft)(String(targetDate.getMilliseconds()), 3, "0"); let type = ""; let datePart = ""; let timePart = ""; // dateFormat = “MDY” dateFormat = “DMY” dateFormat = “YMD” if (dateFormat) { if (dateFormat.indexOf("Y4") === 0) { dateFormat = dateFormat.charAt(0) + dateFormat.charAt(1) + "/" + dateFormat.charAt(2) + "/" + dateFormat.charAt(3); } else { dateFormat = dateFormat.charAt(0) + "/" + dateFormat.charAt(1) + "/" + dateFormat.charAt(2); } } // timeFormat: 12 timeFormat: 24 if (timeFormat === 12) { if (Number(hours) < 12) { type = "AM"; } else { type = "PM"; } hours = String(Number(hours) % 12 || 12); if (hours.toString().length === 1) { hours = "0" + hours; } } // Date Part if ((0, isEmpty_1.isEmpty)(targetDate)) { return ""; } else { switch (dateDigit) { case 0: datePart = ""; break; case 8: if (dateFormat) { year = year.substr(-2); if (dateFormat.indexOf("Y4") === 0) { datePart = dateFormat .replace("D", day.toString()) .replace("M", month.toString()) .replace("Y4", year.toString()); } else { datePart = dateFormat .replace("D", day.toString()) .replace("M", month.toString()) .replace("Y", year.toString()); } } break; case 10: if (dateFormat) { if (dateFormat.indexOf("Y4") === 0) { datePart = dateFormat .replace("D", day.toString()) .replace("M", month.toString()) .replace("Y4", year.toString()); } else { datePart = dateFormat .replace("D", day.toString()) .replace("M", month.toString()) .replace("Y", year.toString()); } } break; default: if (dateFormat) { if (dateFormat.indexOf("Y4") === 0) { datePart = dateFormat .replace("D", day.toString()) .replace("M", month.toString()) .replace("Y4", year.toString()); } else { datePart = dateFormat .replace("D", day.toString()) .replace("M", month.toString()) .replace("Y", year.toString()); } } break; } // Time Part switch (hourDigit) { case 0: timePart = ""; break; case 2: datePart = ""; if (timeFormat === 12) { timePart = `${hours} ${type}`; } else { timePart = `${hours}`; } break; case 5: if (timeFormat === 12) { timePart = `${hours}:${minutes} ${type}`; } else { timePart = `${hours}:${minutes}`; } break; case 8: if (timeFormat === 12) { timePart = `${hours}:${minutes}:${seconds} ${type}`; } else { timePart = `${hours}:${minutes}:${seconds}`; } break; case 12: if (timeFormat === 12) { timePart = `${hours}:${minutes}:${seconds}.${milliseconds} ${type}`; } else { timePart = `${hours}:${minutes}:${seconds}.${milliseconds}`; } break; default: if (timeFormat === 12) { timePart = `${hours}:${minutes}:${seconds} ${type}`; } else { timePart = `${hours}:${minutes}:${seconds}`; } break; } // Return let res = ""; if (datePart !== "" && timePart !== "") { res = `${datePart} ${timePart}`; } if (datePart === "" && timePart !== "") { res = `${timePart}`; } if (timePart === "" && datePart !== "") { res = `${datePart}`; } return res; } }; exports.TtoC = TtoC; //# sourceMappingURL=TtoC.js.map