UNPKG

dayjs-ext

Version:

2KB immutable date time library alternative to Moment.js with the same modern API

3 lines (2 loc) 8.3 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.dayjs=e()}(this,function(){"use strict";function r(){return(r=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}var o=1e3,s="millisecond",c="second",h="minute",f="hour",d="day",$="week",l="month",m="year",y="date",M=/^(\d{4})-?(\d{1,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/,p=/\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,t={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_")},i=function(t,e,n){var r=String(t);return!r||r.length>=e?t:""+Array(e+1-r.length).join(n)+t},e={padStart:i,padZoneStr:function(t){var e=Math.abs(t),n=Math.floor(e/60),r=e%60;return(t<=0?"+":"-")+i(n,2,"0")+":"+i(r,2,"0")},monthDiff:function(t,e){var n=12*(e.year()-t.year())+(e.month()-t.month()),r=t.clone().add(n,"months"),s=e-r<0,i=t.clone().add(n+(s?-1:1),"months"),a=Number(-(n+(e-r)/(s?r-i:i-r)));return 0===Math.abs(a)?0:a},absFloor:function(t){return t<0?Math.ceil(t)||0:Math.floor(t)},prettyUnit:function(t){return{M:l,y:m,w:$,d:d,h:f,m:h,s:c,ms:s}[t]||String(t||"").toLowerCase().replace(/s$/,"")},isUndefined:function(t){return void 0===t}},a="en",u={};u[a]=t;var D=function(t){return t instanceof U},g=function(t,e,n){var r;if(!t)return null;if("string"==typeof t)u[t]&&(r=t),e&&(u[t]=e,r=t);else{var s=t.name;u[s]=t,r=s}return n||(a=r),r},S=function(t,e){if(D(t)){if(!e)return t.clone();t=t.toDate()}var n=e||{};return t instanceof Date&&!1!==n.cloneDate&&(t=new Date(t)),n.date=t,new U(n)},v=function(t,e,n){return S(t,{locale:e.$L,utc:e.$u,cloneDate:n})},b=e;b.parseLocale=g,b.getLocale=function(t){if("string"==typeof t)return u[t];if(t){var e=t.name;u[e]=t}else t=u[a];return t},b.isDayjs=D,b.wrapper=v;var U=function(){function t(t){this.parse(t)}var e=t.prototype;return e.parse=function(t){this.$d=function(t){var e,n=t.date;if(null===n)return new Date(NaN);if(b.isUndefined(n))return new Date;if(n instanceof Date)return n;if("string"==typeof n&&n.charCodeAt(n.length-1)<=57&&(e=n.match(M))){var r=e[1],s=e[2]-1,i=e[3]||1,a=e[5]||0,u=e[6]||0,o=e[7]||0,c=e[8]||0;return t&&t.utc?new Date(Date.UTC(r,s,i,a,u,o,c)):new Date(r,s,i,a,u,o,c)}return new Date(n)}(t),this.init(t)},e.init=function(t){var e=t&&!!t.utc,n=this.$d;this.$u=e,this.$ms=e?(this.$y=n.getUTCFullYear(),this.$M=n.getUTCMonth(),this.$D=n.getUTCDate(),this.$W=n.getUTCDay(),this.$H=n.getUTCHours(),this.$m=n.getUTCMinutes(),this.$s=n.getUTCSeconds(),n.getUTCMilliseconds()):(this.$y=n.getFullYear(),this.$M=n.getMonth(),this.$D=n.getDate(),this.$W=n.getDay(),this.$H=n.getHours(),this.$m=n.getMinutes(),this.$s=n.getSeconds(),n.getMilliseconds()),this.$L=this.$L||g(t.locale,null,!0)||a},e.$utils=function(){return b},e.isValid=function(){return!("Invalid Date"===this.$d.toString())},e.$compare=function(t){return this.valueOf()-S(t).valueOf()},e.isSame=function(t){return 0===this.$compare(t)},e.isBefore=function(t){return this.$compare(t)<0},e.isAfter=function(t){return 0<this.$compare(t)},e.year=function(){return this.$y},e.month=function(){return this.$M},e.day=function(){return this.$W},e.date=function(){return this.$D},e.hour=function(){return this.$H},e.minute=function(){return this.$m},e.second=function(){return this.$s},e.millisecond=function(){return this.$ms},e.unix=function(){return Math.floor(this.valueOf()/1e3)},e.valueOf=function(){return this.$d.getTime()},e.startOf=function(t,e){var s=this,i=!!b.isUndefined(e)||e,n=b.prettyUnit(t),r=this.$u?["setUTCHours","setUTCMinutes","setUTCSeconds","setUTCMilliseconds"]:["setHours","setMinutes","setSeconds","setMilliseconds"],a=function(t,e){var n=s.$u?new Date(Date.UTC(s.$y,e,t)):new Date(s.$y,e,t),r=v(n,s,!1);return i?r:r.endOf(d)},u=function(t){var e=s.toDate();return v(e[r[t]].apply(e,i?[0,0,0,0].slice(t):[23,59,59,999].slice(t)),s,!1)};switch(n){case m:return i?a(1,0):a(31,11);case l:return i?a(1,this.$M):a(0,this.$M+1);case $:return a(i?this.$D-this.$W:this.$D+(6-this.$W),this.$M);case d:case y:return u(0);case f:return u(1);case h:return u(2);case c:return u(3);default:return this.clone()}},e.endOf=function(t){return this.startOf(t,!1)},e.$set=function(t,e){var n=b.prettyUnit(t),r=this.$u?{year:"setUTCFullYear",month:"setUTCMonth",date:"setUTCDate",hours:"setUTCHours",minutes:"setUTCMinutes",seconds:"setUTCSeconds",milliseconds:"setUTCMilliseconds"}:{year:"setFullYear",month:"setMonth",date:"setDate",hours:"setHours",minutes:"setMinutes",seconds:"setSeconds",milliseconds:"setMilliseconds"};switch(n){case d:this.$d[r.date](this.$D+(e-this.$W));break;case y:this.$d[r.date](e);break;case l:this.$d[r.month](e);break;case m:this.$d[r.year](e);break;case f:this.$d[r.hours](e);break;case h:this.$d[r.minutes](e);break;case c:this.$d[r.seconds](e);break;case s:this.$d[r.milliseconds](e)}return this.init(),this},e.set=function(t,e){return this.clone().$set(t,e)},e.add=function(r,t){var s=this;r=Number(r);var e,n=b.prettyUnit(t),i=function(t,e){var n=s.set(y,1).set(t,e+r);return n.set(y,Math.min(s.$D,n.daysInMonth()))},a=function(t){var e=new Date(s.$d);return e.setDate(e.getDate()+t*r),v(e,s)};if(n===l)return i(l,this.$M);if(n===m)return i(m,this.$y);if(n===d)return a(1);if(n===$)return a(7);switch(n){case h:e=6e4;break;case f:e=36e5;break;case c:e=o;break;default:e=1}var u=this.valueOf()+r*e;return v(u,this)},e.subtract=function(t,e){return this.add(-1*t,e)},e.format=function(t){var e=this,n=t||"YYYY-MM-DDTHH:mm:ssZ",r=this.$u?"+00:00":b.padZoneStr(this.$d.getTimezoneOffset()),s=this.$locale(),i=s.weekdays,a=s.months,u=function(t,e,n,r){return t&&t[e]||n[e].substr(0,r)};return n.replace(p,function(t){if(-1<t.indexOf("["))return t.replace(/\[|\]/g,"");switch(t){case"YY":return String(e.$y).slice(-2);case"YYYY":return String(e.$y);case"M":return String(e.$M+1);case"MM":return b.padStart(e.$M+1,2,"0");case"MMM":return u(s.monthsShort,e.$M,a,3);case"MMMM":return a[e.$M];case"D":return String(e.$D);case"DD":return b.padStart(e.$D,2,"0");case"d":return String(e.$W);case"dd":return u(s.weekdaysMin,e.$W,i,2);case"ddd":return u(s.weekdaysShort,e.$W,i,3);case"dddd":return i[e.$W];case"H":return String(e.$H);case"HH":return b.padStart(e.$H,2,"0");case"h":case"hh":return 0===e.$H?12:b.padStart(e.$H<13?e.$H:e.$H-12,"hh"===t?2:1,"0");case"a":return e.$H<12?"am":"pm";case"A":return e.$H<12?"AM":"PM";case"m":return String(e.$m);case"mm":return b.padStart(e.$m,2,"0");case"s":return String(e.$s);case"ss":return b.padStart(e.$s,2,"0");case"SSS":return b.padStart(e.$ms,3,"0");case"Z":return r;default:return r.replace(":","")}})},e.diff=function(t,e,n){var r=b.prettyUnit(e),s=S(t),i=this-s,a=b.monthDiff(this,s);switch(r){case m:a/=12;break;case l:break;case"quarter":a/=3;break;case $:a=i/6048e5;break;case d:a=i/864e5;break;case f:a=i/36e5;break;case h:a=i/6e4;break;case c:a=i/o;break;default:a=i}return n?a:b.absFloor(a)},e.daysInMonth=function(){return this.endOf(l).$D},e.$locale=function(){return u[this.$L]},e.locale=function(t,e){var n=this.clone();return n.$L=g(t,e,!0),n},e.clone=function(){return v(this.toDate(),this,!1)},e.toDate=function(){return new Date(this.$d)},e.toArray=function(){return[this.$y,this.$M,this.$D,this.$H,this.$m,this.$s,this.$ms]},e.toJSON=function(){return this.toISOString()},e.toISOString=function(){return this.$d.toISOString()},e.toObject=function(){return{years:this.$y,months:this.$M,date:this.$D,hours:this.$H,minutes:this.$m,seconds:this.$s,milliseconds:this.$ms}},e.toString=function(){return this.$d.toUTCString()},e.isUTC=function(){return this.$u},e.utc=function(){return S(this.$d.valueOf(),{locale:this.$L,utc:!0})},e.local=function(){return S(this.$d.valueOf(),{locale:this.$L,utc:!1})},e.utcOffset=function(){return this.$u?0:this.$d.getTimezoneOffset()},t}();return S.prototype=U.prototype,S.utc=function(t,e){var n=r({},e,{utc:!0});return S(t,n)},S.extend=function(t,e){return t(e,U,S),S},S.locale=g,S.isDayjs=D,S.unix=function(t){return S(1e3*t)},S.en=u[a],S}); //# sourceMappingURL=dayjs.min.js.map