UNPKG

@akadenia/helpers

Version:
89 lines (88 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatDateTime = exports.getDateString = exports.getReadableDate = exports.getReadableDateTime = void 0; exports.parseDate = parseDate; exports.getShortOrdinalDate = getShortOrdinalDate; /** * * @function * @param {Date} datetime - The datetime that needs to be formatted. * @returns {string} - A string representing the datetime in the format "yyyy-mm-dd hh:mm:ss" */ const getReadableDateTime = (datetime = new Date()) => { const dateString = new Date(datetime.getTime() - datetime.getTimezoneOffset() * 60000); return dateString.toISOString().replace("T", " ").substring(0, 19); }; exports.getReadableDateTime = getReadableDateTime; /** * * @function * @param {Date} datetime - The datetime that needs to be formatted. * @returns {string} - A string representing the date in the format "yyyy-mm-dd" */ const getReadableDate = (datetime = new Date()) => (0, exports.getReadableDateTime)(datetime).substring(0, 10); exports.getReadableDate = getReadableDate; /** * * @function * @param {string | Date} date - The date string that needs to be formatted * @returns {string} - A string representing the date in the user's local timezone */ const getDateString = (date) => new Date(date).toLocaleDateString(); exports.getDateString = getDateString; /** * * @function * @param {string | Date} date - The date string that needs to be formatted * @param {Intl.DateTimeFormatOptions} options - The javascript date formatting options to be used * @param {string | Intl.Locales | undefined} localTimezone - The local timezone to be used * @returns {string} - A string representing the date in the user's local timezone */ const formatDateTime = (date, options, localTimezone = "en-US") => new Date(date).toLocaleDateString(localTimezone, options); exports.formatDateTime = formatDateTime; /** * * @function * @param {string | Date} date - The date string that needs to be returned as a Date object. * @returns {Date} - A date representing the new date object. */ function parseDate(date) { const outputDate = typeof date === "string" ? new Date(date) : date; if (isNaN(outputDate.getTime())) { throw new Error(`Cannot parse date: ${JSON.stringify(date)}`); } return outputDate; } /** * * @function * @param {string | Date} date - The date string that needs to be formatted * @param {boolean} appendTime - A boolean to determine if the time should be appended to the date * @returns {string} - A string representing the date in the short ordinal date */ function getShortOrdinalDate(date, appendTime) { var _a; const newDate = parseDate(date); const day = newDate.getUTCDate(); const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const monthIndex = newDate.getUTCMonth(); const year = newDate.getUTCFullYear(); const hours = newDate.getUTCHours().toString().padStart(2, "0"); const minutes = newDate.getUTCMinutes().toString().padStart(2, "0"); const seconds = newDate.getUTCSeconds().toString().padStart(2, "0"); const ordinalEndings = ["th", "st", "nd", "rd"]; let suffix = ordinalEndings[0]; if (day <= 3 && day > 0) { suffix = ordinalEndings[day]; } else { // Determine the ordinal endings based on the last digit of the day suffix = (_a = ordinalEndings[day % 10]) !== null && _a !== void 0 ? _a : suffix; } if (appendTime) { return `${monthNames[monthIndex]} ${day}${suffix} ${year} ${hours}:${minutes}:${seconds}`; } else { return `${monthNames[monthIndex]} ${day}${suffix} ${year}`; } }