UNPKG

@kubit-ui-web/react-components

Version:

Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience

68 lines 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatDateToUTC = exports.formatDate = void 0; const common_constant_1 = require("./constants/common.constant"); const format_constant_1 = require("./constants/format.constant"); const locale_1 = require("./locale"); const validateDate_1 = require("./validateDate"); const addZero = (value) => `${value < 10 ? '0' : ''}${value}`; const formatDate = (date, format) => { const formatAux = typeof format === 'string' ? format_constant_1.MACRO_TOKEN_TO_FORMAT_OPTS[format] : format; if (typeof format === 'string' && !formatAux) { const formatParts = format.match(common_constant_1.FORMATTING_TOKENS) || []; const matches = { YY: ['year', (value) => `${new Date(value).getFullYear()}`.slice(-2)], YYYY: ['year', (value) => `${new Date(value).getFullYear()}`], M: ['month', (value) => `${new Date(value).getMonth() + 1}`], MM: ['month', (value) => `${addZero(new Date(value).getMonth() + 1)}`], MMM: [ 'month', (value) => new Date(value).toLocaleString(locale_1.locale.getLocale(), { month: 'short' }), ], MMMM: [ 'month', (value) => new Date(value).toLocaleString(locale_1.locale.getLocale(), { month: 'long' }), ], D: ['day', (value) => `${new Date(value).getDate()}`], DD: ['day', (value) => `${addZero(new Date(value).getDate())}`], W: [ 'weekday', (value) => new Date(value).toLocaleString(locale_1.locale.getLocale(), { weekday: 'short' }), ], WW: [ 'weekday', (value) => new Date(value).toLocaleString(locale_1.locale.getLocale(), { weekday: 'long' }), ], H: ['hour', (value) => `${new Date(value).getHours()}`], HH: ['hour', (value) => `${addZero(new Date(value).getHours())}`], m: ['minute', (value) => `${new Date(value).getMinutes()}`], mm: ['minute', (value) => `${addZero(new Date(value).getMinutes())}`], s: ['second', (value) => `${new Date(value).getSeconds()}`], ss: ['second', (value) => `${addZero(new Date(value).getSeconds())}`], }; const values = formatParts.map((token) => { const parseTo = matches[token]; const key = parseTo && parseTo[0]; const value = parseTo && parseTo[1](date); return key ? value : token; }); return values.join('').replace(/[\\[\]]/g, ''); } return date.toLocaleString(locale_1.locale.getLocale(), { ...formatAux, }); }; exports.formatDate = formatDate; const formatDateToUTC = (date) => { const dateObj = new Date(date); if (!(0, validateDate_1.isValidDate)(dateObj)) { throw Error('Date is invalid'); } // If the date object has a time of 00:00:00, return the date object because it is already in UTC if (dateObj.getHours() === 0 && dateObj.getMinutes() === 0 && dateObj.getSeconds() === 0) { return dateObj; } return new Date(new Intl.DateTimeFormat('en-US', { timeZone: 'UTC' }).format(new Date(date))); }; exports.formatDateToUTC = formatDateToUTC; //# sourceMappingURL=formatDate.js.map