UNPKG

date-time-plugin-minify

Version:

A date time pulgin modify date and time in easy formats

264 lines 9.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DateFormatter = void 0; exports.GetFormattedDate = GetFormattedDate; exports.getDiffBetweenDates = getDiffBetweenDates; class DateFormatter { date; format; formatted; constructor(date, format, currentFormat) { if (typeof date === 'string' && currentFormat) { switch (currentFormat) { case 'YYYY-MM-DD': { const [year, month, day] = date.split('-').map(Number); this.date = new Date(year, month - 1, day); break; } case 'DD/MM/YYYY': { const [day, month, year] = date.split('/').map(Number); this.date = new Date(year, month - 1, day); break; } case 'MM/DD/YYYY': { const [month, day, year] = date.split('/').map(Number); this.date = new Date(year, month - 1, day); break; } case 'MM-DD-YYYY': { const [month, day, year] = date.split('-').map(Number); this.date = new Date(year, month - 1, day); break; } case 'DD MMM, YYYY': { const [day, monthStr, year] = date.replace(',', '').split(' '); const monthMap = new Map(Array.from(getMonthMap().entries()).map(([k, v]) => [v.toLowerCase(), k])); const month = monthMap.get(monthStr.toLowerCase()); if (!month) throw new Error(`Invalid month: ${monthStr}`); this.date = new Date(Number(year), month - 1, Number(day)); break; } case 'YYYY/MM/DD': { const [year, month, day] = date.split('/').map(Number); this.date = new Date(year, month - 1, day); break; } case 'YYYY/MM/DD HH:mm:ss': { const [datePart, timePart] = date.split(' '); const [year, month, day] = datePart.split('/').map(Number); const [hours, minutes, seconds] = timePart.split(':').map(Number); this.date = new Date(year, month - 1, day, hours, minutes, seconds); break; } case 'HH:mm:ss': { const [hours, minutes, seconds] = date.split(':').map(Number); const now = new Date(); this.date = new Date(now.getFullYear(), now.getMonth(), now.getDate(), hours, minutes, seconds); break; } default: throw new Error(`Unsupported currentFormat: ${currentFormat}`); } } else { this.date = new Date(date); } this.format = format; } formatDate() { const d = this.date; const format = this.format; const map = { YYYY: d.getFullYear().toString(), [format.includes("MMM") ? 'MMM' : 'MM']: format.includes("MMM") ? getMonthMap().get(d.getMonth() + 1) : String(d.getMonth() + 1).padStart(2, '0'), DD: String(d.getDate()).padStart(2, '0'), HH: String(d.getHours()).padStart(2, '0'), mm: String(d.getMinutes()).padStart(2, '0'), ss: String(d.getSeconds()).padStart(2, '0'), }; let formattedPattern = format; for (const key in map) { formattedPattern = formattedPattern.replace(key, map[key]); } this.formatted = formattedPattern; return this; } TZ(timeZone, locale = 'en-US') { return new Intl.DateTimeFormat(locale, { timeZone, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }).format(this.date); } getFormatted() { return this.formatted ?? this.date.toString(); } } exports.DateFormatter = DateFormatter; function getMonthMap() { const map = new Map(); map.set(1, "Jan"); map.set(2, "Feb"); map.set(3, "Mar"); map.set(4, "April"); map.set(5, "May"); map.set(6, "June"); map.set(7, "July"); map.set(8, "Aug"); map.set(9, "Sept"); map.set(10, "Oct"); map.set(11, "Nov"); map.set(12, "Dec"); return map; } function GetFormattedDate(date, format) { try { if (!date) { throw new Error("date is missing"); } if (!format) { this.date = new Date(date); } else { this.date = new DateFormatter(date, format).formatDate().getFormatted(); } } catch (error) { throw new Error("Date is not provided", { cause: error.message }); } } GetFormattedDate.prototype.addDays = function (days) { try { if (!days) { throw new Error('Days are not provide in Function'); } this.date.setDate(this.date.getDate() + days); return this; } catch (error) { throw new Error("Unable to add days as", { cause: error.message }); } }; /** * Add months in current date and return new date * ex : new GetFormattedDate(new Date()).addMonth(3) // return new date with 3 month added */ GetFormattedDate.prototype.addMonths = function (months) { try { if (!months) { throw new Error('Months are not provide in Function'); } this.date.setMonth(this.date.getMonth() + months); return this; } catch (error) { throw new Error("Unable to add months as", { cause: error.message }); } }; GetFormattedDate.prototype.addYear = function (years) { try { if (!years) { throw new Error('years are not provide in Function'); } this.date.setFullYear(this.date.getFullYear() + years); return this; } catch (error) { throw new Error("Unable to add years as", { cause: error.message.message }); } }; GetFormattedDate.prototype.toLocaleString = function (locale, zone) { try { if (!locale) throw new Error("locale is missin"); } catch (error) { throw new Error("locale is missing", { cause: error.message }); } if (!zone) return this.date.toLocaleString(locale, { timeZone: "UTC" }); return this.date.toLocaleString(locale, { timeZone: zone }); }; function getDiffBetweenDates(firstDate, secondDate, options) { try { if (!firstDate) throw new Error('First date is missing'); if (!secondDate) throw new Error('Second date is missing'); if (typeof firstDate !== 'string' && !(firstDate instanceof Date)) { throw new Error('First date must be a string or Date object'); } if (typeof secondDate !== 'string' && !(secondDate instanceof Date)) { throw new Error('Second date must be a string or Date object'); } if (!options) { let fd, sd; if (typeof firstDate === 'string') { const [day, month, year] = firstDate.split('/'); fd = new Date(`${year}-${month}-${day}`); } else { fd = new Date(firstDate); } if (typeof secondDate === 'string') { const [day, month, year] = secondDate.split('/'); sd = new Date(`${year}-${month}-${day}`); } else { sd = new Date(secondDate); } return calculateDiff(fd, sd); } if (options && options.format) { const formatArr = ['YYYY-MM-DD', 'DD/MM/YYYY', 'MM/DD/YYYY', 'MM-DD-YYYY', 'DD MMM, YYYY', 'YYYY/MM/DD', 'YYYY/MM/DD HH:mm:ss']; if (formatArr.indexOf(options.format) !== -1) { let fd = new DateFormatter(firstDate, options.format).formatDate().getFormatted(); let sd = new DateFormatter(secondDate, options.format).formatDate().getFormatted(); return calculateDiff(fd, sd); } else { throw new Error("Date format is not supported"); } } } catch (error) { console.error(error); return null; } } function calculateDiff(fd, sd) { fd = new Date(fd); sd = new Date(sd); if (fd > sd) [fd, sd] = [sd, fd]; // Always fd <= sd const diffTime = sd - fd; const diffSec = diffTime / 1000; const diffMin = diffSec / 60; const diffHours = diffMin / 60; const diffDays = diffHours / 24; let years = sd.getFullYear() - fd.getFullYear(); let months = sd.getMonth() - fd.getMonth(); if (sd.getDate() < fd.getDate()) { months--; } if (months < 0) { years--; months += 12; } return { seconds: diffSec, minutes: diffMin, hours: diffHours, days: diffDays, months: years * 12 + months, years: years }; } //# sourceMappingURL=index.js.map