UNPKG

@yookue/ts-lang-utils

Version:

Common lang utilities for typescript

57 lines (55 loc) 2 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/util/DateUtils/formatDateTime.ts var formatDateTime_exports = {}; __export(formatDateTime_exports, { formatDateTime: () => formatDateTime }); module.exports = __toCommonJS(formatDateTime_exports); function formatDateTime(date, format) { if (!date || !format) { return void 0; } const entries = { "y+": date.getFullYear(), "M+": date.getMonth() + 1, "d+": date.getDate(), "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, "H+": date.getHours(), "m+": date.getMinutes(), "s+": date.getSeconds(), "q+": Math.floor((date.getMonth() + 3) / 3), "S": date.getMilliseconds() }; let result = format; for (const entry in entries) { const array = new RegExp(`(${entry})`).exec(result); if (array) { if (/(y+)/.test(entry)) { result = result.replace(array[1], entries[entry].toString().substring(4 - array[1].length)); } else { result = result.replace(array[1], array[1].length === 1 ? entries[entry].toString() : entries[entry].toString().padStart(array[1].length, "0")); } } } return result; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { formatDateTime });