UNPKG

light-date

Version:

Blazing fast & lightweight (180 bytes) date formatting for Node.js and the browser.

45 lines (44 loc) 1.67 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.localeFormat = exports.format = void 0; /** * Use this API for simple, most common formatting. * * @param {Date} date - Date object, which should be used. * @param {string} exp - String, which you want to format, for example: `{yyyy}-{MM}-{dd}` or Current time: `{hh}:{mm}:{ss}`. * @return {string} String with formatted date. * * @example * format(new Date(2014, 1, 11), '{yyyy}-{MM}-{dd}') //=> '2014-01-11' */ exports.format = (date, exp) => exp.replace(/\\?{.*?}/g, key => { if (key.startsWith('\\')) { return key.slice(1); } switch (key) { case '{yyyy}': return `${date.getFullYear()}`; case '{yy}': return `${date.getFullYear()}`.slice(-2); case '{MM}': return `${(date.getMonth() + 1)}`.padStart(2, '0'); case '{dd}': return `${date.getDate()}`.padStart(2, '0'); case '{HH}': return `${date.getHours()}`.padStart(2, '0'); case '{mm}': return `${date.getMinutes()}`.padStart(2, '0'); case '{ss}': return `${date.getSeconds()}`.padStart(2, '0'); case '{SSS}': return `${date.getMilliseconds()}`.padStart(3, '0'); default: return ''; } }); /* c8 ignore next */ var locale_1 = require("./locale"); Object.defineProperty(exports, "localeFormat", { enumerable: true, get: function () { return __importDefault(locale_1).default; } });