intl-date
Version:
Javascript library to represent and work with dates in different calendar systems (e.g. hijri, persian and others).
3 lines (2 loc) • 3.38 kB
JavaScript
var t=function(t,e){var n,r,a,i=new Intl.DateTimeFormat("en-u-ca-"+t,{day:"numeric",month:"numeric",year:"numeric"}).formatToParts(e);return[parseInt((null==(n=i.find(function(t){return"year"===t.type}))?void 0:n.value)+""),parseInt((null==(r=i.find(function(t){return"month"===t.type}))?void 0:r.value)+""),parseInt((null==(a=i.find(function(t){return"day"===t.type}))?void 0:a.value)+"")]},e=function e(n,r,a,i){var o=this;this.jsDate=void 0,this.converted={},this._int=void 0,this.validateSupportedCalendarType=function(t){switch(t){case"gregorian":case"islamic":case"islamic-umalqura":case"islamic-rgsa":case"islamic-civil":case"islamic-tbla":case"persian":return;default:throw"Unsupported calendar type "+t}},this.getConverted=function(e){var n=o.converted[e];return n||(n=o.converted[e]=t(e,o.jsDate)),n},this.getYear=function(t){return o.validateSupportedCalendarType(t),"gregorian"===t?o.jsDate.getFullYear():o.getConverted(t)[0]},this.getMonth=function(t){return o.validateSupportedCalendarType(t),"gregorian"===t?o.jsDate.getMonth()+1:o.getConverted(t)[1]},this.getDay=function(t){return o.validateSupportedCalendarType(t),"gregorian"===t?o.jsDate.getDate():o.getConverted(t)[2]},this.getDayOfWeek=function(){return o.jsDate.getDay()+1},this.getQuarter=function(t){o.validateSupportedCalendarType(t);var e=o.getMonth(t);return Math.floor((e-1)/3)+1},this.isEqual=function(t){return o._int===t._int},this.isBefore=function(t){return o._int<t._int},this.isAfter=function(t){return o._int>t._int},this.isBetween=function(t,e){return o._int>t._int&&o._int<e._int||o._int>e._int&&o._int<t._int},this.daysUntil=function(t){return Math.ceil((t._int-o._int)/864e5)},this.plusDays=function(t){var n=new Date(o.jsDate);return n.setDate(n.getDate()+t),e.of("gregorian",n.getFullYear(),n.getMonth()+1,n.getDate())},this.minusDays=function(t){return o.plusDays(-1*t)},this.format=function(t,e){o.validateSupportedCalendarType(t);var n=o.getYear(t).toString(),r=o.getMonth(t).toString(),a=o.getDay(t).toString();return e.replace(/yyyy/g,n.padStart(4,"0")).replace(/yy/g,n.slice(-2).padStart(2,"0")).replace(/y/g,n).replace(/MM/g,r.padStart(2,"0")).replace(/M/g,r).replace(/dd/g,a.padStart(2,"0")).replace(/d/g,a)},this.toString=function(t){return o.validateSupportedCalendarType(t),o.format(t,"yyyy-MM-dd")},this.validateSupportedCalendarType(n),"gregorian"===n?this.jsDate=new Date(r,a-1,i):(this.converted[n]=[r,a,i],this.jsDate=function(e,n,r,a){var i=new Date;i.setHours(0),i.setMinutes(0),i.setSeconds(0),i.setMilliseconds(0);for(var o=t(e,i),s=0;o[0]!==n||o[1]!==r||o[2]!==a;){if(++s>50)throw"toGregorian: Could not find a conversion within the defined max iterations limit.";var u=365*n+30*r+a-(365*o[0]+30*o[1]+o[2]);i.setDate(i.getDate()+u),o=t(e,i)}return i}(n,r,a,i)),this._int=this.jsDate.getTime()};e.of=function(t,n,r,a){return new e(t,n,r,a)},e.from=function(t){return e.of("gregorian",t.getFullYear(),t.getMonth()+1,t.getDate())},e.parse=function(t,n){if(!n||n.length>10)throw"Invalid date string "+n;var r=n.split("-");if(1===r.length&&(r=n.split("/")),3!=r.length)throw"Invalid date string "+n;var a=parseInt(r[0]),i=parseInt(r[1]),o=parseInt(r[2]);return e.of(t,a,i,o)},e.today=function(){return e.from(new Date)},e.min=function(t,e){return t.isBefore(e)?t:e},e.max=function(t,e){return t.isAfter(e)?t:e};export{e as IntlDate};
//# sourceMappingURL=index.esm.js.map