UNPKG

date-time-plugin-minify

Version:

A date time pulgin modify date and time in easy formats

1 lines 4.79 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DateFormatter=void 0,exports.GetFormattedDate=GetFormattedDate,exports.getDiffBetweenDates=getDiffBetweenDates;class DateFormatter{date;format;formatted;constructor(t,e,r){if("string"==typeof t&&r)switch(r){case"YYYY-MM-DD":{const[e,r,a]=t.split("-").map(Number);this.date=new Date(e,r-1,a);break}case"DD/MM/YYYY":{const[e,r,a]=t.split("/").map(Number);this.date=new Date(a,r-1,e);break}case"MM/DD/YYYY":{const[e,r,a]=t.split("/").map(Number);this.date=new Date(a,e-1,r);break}case"MM-DD-YYYY":{const[e,r,a]=t.split("-").map(Number);this.date=new Date(a,e-1,r);break}case"DD MMM, YYYY":{const[e,r,a]=t.replace(",","").split(" "),o=new Map(Array.from(getMonthMap().entries()).map((([t,e])=>[e.toLowerCase(),t]))).get(r.toLowerCase());if(!o)throw new Error(`Invalid month: ${r}`);this.date=new Date(Number(a),o-1,Number(e));break}case"YYYY/MM/DD":{const[e,r,a]=t.split("/").map(Number);this.date=new Date(e,r-1,a);break}case"YYYY/MM/DD HH:mm:ss":{const[e,r]=t.split(" "),[a,o,s]=e.split("/").map(Number),[n,i,D]=r.split(":").map(Number);this.date=new Date(a,o-1,s,n,i,D);break}case"HH:mm:ss":{const[e,r,a]=t.split(":").map(Number),o=new Date;this.date=new Date(o.getFullYear(),o.getMonth(),o.getDate(),e,r,a);break}default:throw new Error(`Unsupported currentFormat: ${r}`)}else this.date=new Date(t);this.format=e}formatDate(){const t=this.date,e=this.format,r={YYYY:t.getFullYear().toString(),[e.includes("MMM")?"MMM":"MM"]:e.includes("MMM")?getMonthMap().get(t.getMonth()+1):String(t.getMonth()+1).padStart(2,"0"),DD:String(t.getDate()).padStart(2,"0"),HH:String(t.getHours()).padStart(2,"0"),mm:String(t.getMinutes()).padStart(2,"0"),ss:String(t.getSeconds()).padStart(2,"0")};let a=e;for(const t in r)a=a.replace(t,r[t]);return this.formatted=a,this}TZ(t,e="en-US"){return new Intl.DateTimeFormat(e,{timeZone:t,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()}}function getMonthMap(){const t=new Map;return t.set(1,"Jan"),t.set(2,"Feb"),t.set(3,"Mar"),t.set(4,"April"),t.set(5,"May"),t.set(6,"June"),t.set(7,"July"),t.set(8,"Aug"),t.set(9,"Sept"),t.set(10,"Oct"),t.set(11,"Nov"),t.set(12,"Dec"),t}function GetFormattedDate(t,e){try{if(!t)throw new Error("date is missing");this.date=e?new DateFormatter(t,e).formatDate().getFormatted():new Date(t)}catch(t){throw new Error("Date is not provided",{cause:t.message})}}function getDiffBetweenDates(t,e,r){try{if(!t)throw new Error("First date is missing");if(!e)throw new Error("Second date is missing");if("string"!=typeof t&&!(t instanceof Date))throw new Error("First date must be a string or Date object");if("string"!=typeof e&&!(e instanceof Date))throw new Error("Second date must be a string or Date object");if(!r){let r,a;if("string"==typeof t){const[e,a,o]=t.split("/");r=new Date(`${o}-${a}-${e}`)}else r=new Date(t);if("string"==typeof e){const[t,r,o]=e.split("/");a=new Date(`${o}-${r}-${t}`)}else a=new Date(e);return calculateDiff(r,a)}if(r&&r.format){if(-1!==["YYYY-MM-DD","DD/MM/YYYY","MM/DD/YYYY","MM-DD-YYYY","DD MMM, YYYY","YYYY/MM/DD","YYYY/MM/DD HH:mm:ss"].indexOf(r.format)){return calculateDiff(new DateFormatter(t,r.format).formatDate().getFormatted(),new DateFormatter(e,r.format).formatDate().getFormatted())}throw new Error("Date format is not supported")}}catch(t){return console.error(t),null}}function calculateDiff(t,e){(t=new Date(t))>(e=new Date(e))&&([t,e]=[e,t]);const r=(e-t)/1e3,a=r/60,o=a/60,s=o/24;let n=e.getFullYear()-t.getFullYear(),i=e.getMonth()-t.getMonth();return e.getDate()<t.getDate()&&i--,i<0&&(n--,i+=12),{seconds:r,minutes:a,hours:o,days:s,months:12*n+i,years:n}}exports.DateFormatter=DateFormatter,GetFormattedDate.prototype.addDays=function(t){try{if(!t)throw new Error("Days are not provide in Function");return this.date.setDate(this.date.getDate()+t),this}catch(t){throw new Error("Unable to add days as",{cause:t.message})}},GetFormattedDate.prototype.addMonths=function(t){try{if(!t)throw new Error("Months are not provide in Function");return this.date.setMonth(this.date.getMonth()+t),this}catch(t){throw new Error("Unable to add months as",{cause:t.message})}},GetFormattedDate.prototype.addYear=function(t){try{if(!t)throw new Error("years are not provide in Function");return this.date.setFullYear(this.date.getFullYear()+t),this}catch(t){throw new Error("Unable to add years as",{cause:t.message.message})}},GetFormattedDate.prototype.toLocaleString=function(t,e){try{if(!t)throw new Error("locale is missin")}catch(t){throw new Error("locale is missing",{cause:t.message})}return e?this.date.toLocaleString(t,{timeZone:e}):this.date.toLocaleString(t,{timeZone:"UTC"})};