date-and-time
Version:
The simplest, most intuitive date and time library
3 lines (2 loc) • 13.4 kB
JavaScript
;
/** @preserve Copyright (c) KNOWLEDGECODE - MIT License */const e=/\\\[/g,t=/\\\]/g,r=/\uE000/g,n=/\uE001/g,s=/\[(?:[^[\]]|\[[^[\]]*])*]|([A-Za-z])\1*|\.{3}|./g,o=o=>{const a=o.replace(e,"").replace(t,"").match(s)??[];return[o,...a.map(e=>e.replace(r,"[").replace(n,"]"))]},a=new Map,i=e=>a.get(e)??(()=>{const t=new Intl.DateTimeFormat("en-US",{hour12:!1,weekday:"short",year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",fractionalSecondDigits:3,timeZone:e});return e&&a.set(e,t),t})(),c=(e,t)=>{if("UTC"===t.toUpperCase())return{weekday:e.getUTCDay(),year:e.getUTCFullYear(),month:e.getUTCMonth()+1,day:e.getUTCDate(),hour:e.getUTCHours(),minute:e.getUTCMinutes(),second:e.getUTCSeconds(),fractionalSecond:e.getUTCMilliseconds(),timezoneOffset:0};const r=i(t).formatToParts(e).reduce((e,{type:t,value:r})=>{switch(t){case"weekday":e[t]="SunMonTueWedThuFriSat".indexOf(r)/3;break;case"hour":e[t]=+r%24;break;case"year":case"month":case"day":case"minute":case"second":case"fractionalSecond":e[t]=+r}return e},{weekday:4,year:1970,month:1,day:1,hour:0,minute:0,second:0,fractionalSecond:0,timezoneOffset:0});return r.timezoneOffset=(e.getTime()-Date.UTC(r.year,r.month-(r.year<100?22801:1),r.day,r.hour,r.minute,r.second,r.fractionalSecond))/6e4,r},u=e=>Date.UTC(e.year,e.month-(e.year<100?22801:1),e.day,e.hour,e.minute,e.second,e.fractionalSecond+6e4*e.timezoneOffset);class l{constructor(e,t){this.parts=c(e,t),this.time=e.getTime()}getFullYear(){return this.parts.year}getMonth(){return this.parts.month-1}getDate(){return this.parts.day}getHours(){return this.parts.hour}getMinutes(){return this.parts.minute}getSeconds(){return this.parts.second}getMilliseconds(){return this.parts.fractionalSecond}getDay(){return this.parts.weekday}getTime(){return this.time}getTimezoneOffset(){return this.parts.timezoneOffset}}class d{}const h=(e,t)=>e.getFullYear()+("buddhist"===t?543:0);const m=new class extends d{YYYY(e,t){return`000${String(h(e,t.calendar))}`.slice(-4)}YY(e,t){return`0${String(h(e,t.calendar))}`.slice(-2)}Y(e,t){return String(h(e,t.calendar))}MMMM(e,t,r){return t.locale.getMonthList({style:"long",compiledObj:r})[e.getMonth()]||""}MMM(e,t,r){return t.locale.getMonthList({style:"short",compiledObj:r})[e.getMonth()]||""}MM(e){return`0${String(e.getMonth()+1)}`.slice(-2)}M(e){return String(e.getMonth()+1)}DD(e){return`0${String(e.getDate())}`.slice(-2)}D(e){return String(e.getDate())}HH(e,t){return`0${String(e.getHours()||("h24"===t.hour24?24:0))}`.slice(-2)}H(e,t){return String(e.getHours()||("h24"===t.hour24?24:0))}AA(e,t,r){return t.locale.getMeridiemList({style:"long",compiledObj:r,case:"uppercase"})[+(e.getHours()>11)]||""}A(e,t,r){return t.locale.getMeridiemList({style:"short",compiledObj:r,case:"uppercase"})[+(e.getHours()>11)]||""}aa(e,t,r){return t.locale.getMeridiemList({style:"long",compiledObj:r,case:"lowercase"})[+(e.getHours()>11)]||""}a(e,t,r){return t.locale.getMeridiemList({style:"short",compiledObj:r,case:"lowercase"})[+(e.getHours()>11)]||""}hh(e,t){return`0${String(e.getHours()%12||("h12"===t.hour12?12:0))}`.slice(-2)}h(e,t){return String(e.getHours()%12||("h12"===t.hour12?12:0))}mm(e){return`0${String(e.getMinutes())}`.slice(-2)}m(e){return String(e.getMinutes())}ss(e){return`0${String(e.getSeconds())}`.slice(-2)}s(e){return String(e.getSeconds())}SSS(e){return`00${String(e.getMilliseconds())}`.slice(-3)}SS(e){return`00${String(e.getMilliseconds())}`.slice(-3,-1)}S(e){return`00${String(e.getMilliseconds())}`.slice(-3,-2)}dddd(e,t,r){return t.locale.getDayOfWeekList({style:"long",compiledObj:r})[e.getDay()]||""}ddd(e,t,r){return t.locale.getDayOfWeekList({style:"short",compiledObj:r})[e.getDay()]||""}dd(e,t,r){return t.locale.getDayOfWeekList({style:"narrow",compiledObj:r})[e.getDay()]||""}Z(e){const t=e.getTimezoneOffset(),r=Math.abs(t);return`${t>0?"-":"+"}${`0${String(r/60|0)}`.slice(-2)}${`0${String(r%60)}`.slice(-2)}`}ZZ(e){const t=e.getTimezoneOffset(),r=Math.abs(t);return`${t>0?"-":"+"}${`0${String(r/60|0)}`.slice(-2)}:${`0${String(r%60)}`.slice(-2)}`}},g={MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],MMM:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],dddd:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],ddd:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],dd:["Su","Mo","Tu","We","Th","Fr","Sa"],A:["AM","PM"],AA:["A.M.","P.M."],a:["am","pm"],aa:["a.m.","p.m."]};var M=new class{getLocale(){return"en"}getMonthList(e){return"long"===e.style?g.MMMM:g.MMM}getDayOfWeekList(e){return"long"===e.style?g.dddd:"short"===e.style?g.ddd:g.dd}getMeridiemList(e){return"long"===e.style?"lowercase"===e.case?g.aa:g.AA:"lowercase"===e.case?g.a:g.A}},f={encode:e=>e,decode:e=>e};const p=e=>"object"==typeof e&&"zone_name"in e&&"gmt_offset"in e,y=e=>"UTC"===e,S=(e,t)=>new Date(e-1e3*((e,t)=>{const r=i("UTC"),n=i(t.zone_name),s=t.gmt_offset;for(let t=0;t<2;t++){const o=r.format(e-86400*t*1e3);for(let r=0,a=s.length;r<a;r++)if(n.format(e-1e3*(s[r]+86400*t))===o)return s[r]}return NaN})(e,t)),D=/^\[(.*)\]$/;function T(e,t,r){const n=("string"==typeof t?o(t):t).slice(1),s=p(r?.timeZone)||y(r?.timeZone)?r.timeZone:void 0,a="string"==typeof s?s:s?.zone_name??"",i=a?new l(e,a):e,c={hour12:r?.hour12??"h12",hour24:r?.hour24??"h23",numeral:r?.numeral??f,calendar:r?.calendar??"gregory",timeZone:s,locale:r?.locale??M},u=[...r?.plugins??[],m],d=c.numeral.encode;return n.reduce((e,t)=>e+((e,t)=>{for(const r of u)if(r[e])return d(r[e](i,c,t));return D.test(e)?e.replace(D,"$1"):e})(t,n),"")}class L{}const C=(e,t,r)=>{const n=e.exec(t)?.[0]??"";return{value:+n,length:n.length,token:r}},w=(e,t,r)=>e.reduce((e,n,s)=>n.length>e.length&&!t.indexOf(n)?{value:s,length:n.length,token:r}:e,{value:-1,length:0,token:r});const b=new class extends L{YYYY(e){return C(/^\d{4}/,e,"Y")}Y(e){return C(/^\d{1,4}/,e,"Y")}MMMM(e,t,r){const n=t.locale.getMonthList({style:"long",compiledObj:r}),s=t.locale.getLocale(),o=t.ignoreCase?w(n.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"M"):w(n,e,"M");return o.value++,o}MMM(e,t,r){const n=t.locale.getMonthList({style:"short",compiledObj:r}),s=t.locale.getLocale(),o=t.ignoreCase?w(n.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"M"):w(n,e,"M");return o.value++,o}MM(e){return C(/^\d\d/,e,"M")}M(e){return C(/^\d\d?/,e,"M")}DD(e){return C(/^\d\d/,e,"D")}D(e){return C(/^\d\d?/,e,"D")}HH(e){return C(/^\d\d/,e,"H")}H(e){return C(/^\d\d?/,e,"H")}AA(e,t,r){const n=t.locale.getMeridiemList({style:"long",compiledObj:r,case:"uppercase"}),s=t.locale.getLocale();return t.ignoreCase?w(n.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):w(n,e,"A")}A(e,t,r){const n=t.locale.getMeridiemList({style:"short",compiledObj:r,case:"uppercase"}),s=t.locale.getLocale();return t.ignoreCase?w(n.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):w(n,e,"A")}aa(e,t,r){const n=t.locale.getMeridiemList({style:"long",compiledObj:r,case:"lowercase"}),s=t.locale.getLocale();return t.ignoreCase?w(n.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):w(n,e,"A")}a(e,t,r){const n=t.locale.getMeridiemList({style:"short",compiledObj:r,case:"lowercase"}),s=t.locale.getLocale();return t.ignoreCase?w(n.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):w(n,e,"A")}hh(e){return C(/^\d\d/,e,"h")}h(e){return C(/^\d\d?/,e,"h")}mm(e){return C(/^\d\d/,e,"m")}m(e){return C(/^\d\d?/,e,"m")}ss(e){return C(/^\d\d/,e,"s")}s(e){return C(/^\d\d?/,e,"s")}SSS(e){return C(/^\d{1,3}/,e,"S")}SS(e){const t=C(/^\d\d?/,e,"S");return t.value*=10,t}S(e){const t=C(/^\d/,e,"S");return t.value*=100,t}Z(e){const t=C(/^[+-][01]\d[0-5]\d/,e,"Z");return t.value=-60*(t.value/100|0)-t.value%100,t}ZZ(e){const t=/^([+-][01]\d):([0-5]\d)/.exec(e)??["","",""],r=+(t[1]+t[2]);return{value:-60*(r/100|0)-r%100,length:t[0].length,token:"Z"}}},O=/^\[(.*)\]$/;function x(e,t,r){const n=("string"==typeof t?o(t):t).slice(1),s={hour12:r?.hour12??"h12",hour24:r?.hour24??"h23",numeral:r?.numeral??f,calendar:r?.calendar??"gregory",ignoreCase:r?.ignoreCase??!1,timeZone:p(r?.timeZone)||y(r?.timeZone)?r.timeZone:void 0,locale:r?.locale??M},a={_index:0,_length:0,_match:0},i=[...r?.plugins??[],b],c=(e,t)=>{for(const r of i)if(r[e])return r[e](t,s,n)};e=s.numeral.decode(e);for(const t of n){const r=e.substring(a._index),n=c(t,r);if(n){if(!n.length)break;n.token&&(a[n.token]=n.value+0),a._index+=n.length,a._match++}else if(t===r[0]||" "===t)a._index++;else{if(!O.test(t)||r.indexOf(t.replace(O,"$1"))){if("..."===t){a._index=e.length;break}break}a._index+=t.length-2}}return a._length=e.length,a}function v(e,t){const r=void 0===e.Y?1970:e.Y-("buddhist"===t?.calendar?543:0),[n,s]="h11"===t?.hour12?[0,11]:[1,12],[o,a]="h24"===t?.hour24?[1,24]:[0,23],i=(e,t,r)=>void 0===e||e>=t&&e<=r;return e._index>0&&e._length>0&&e._index===e._length&&e._match>0&&i(r,1,9999)&&i(e.M,1,12)&&i(e.D,1,(c=r,u=e.M??1,new Date(c,u-(c<100?22800:0),0).getDate()))&&i(e.H,o,a)&&i(e.A,0,1)&&i(e.h,n,s)&&i(e.m,0,59)&&i(e.s,0,59)&&i(e.S,0,999)&&i(e.Z,-840,720);var c,u}function A(e,t,r){const n=x(e,t,r);return v(n,r)?(n.Y=n.Y?n.Y-("buddhist"===r?.calendar?543:0):1970,n.M=(n.M??1)-(n.Y<100?22801:1),n.D??=1,n.H=(n.H??0)%24||12*(n.A??0)+(n.h??0)%12,n.m??=0,n.s??=0,n.S??=0,p(r?.timeZone)?S(Date.UTC(n.Y,n.M,n.D,n.H,n.m,n.s,n.S),r.timeZone):y(r?.timeZone)||"Z"in n?new Date(Date.UTC(n.Y,n.M,n.D,n.H,n.m+(n.Z??0),n.s,n.S)):new Date(n.Y,n.M,n.D,n.H,n.m,n.s,n.S)):new Date(NaN)}function U(e,t,r){if(p(r)){const n=c(e,r.zone_name);n.month+=t,n.timezoneOffset=0;const s=new Date(u(n));return s.getUTCDate()<n.day&&s.setUTCDate(0),S(u({...n,year:s.getUTCFullYear(),month:s.getUTCMonth()+1,day:s.getUTCDate()}),r)}const n=new Date(e.getTime());return y(r)?(n.setUTCMonth(n.getUTCMonth()+t),n.getUTCDate()<e.getUTCDate()?(n.setUTCDate(0),n):n):(n.setMonth(n.getMonth()+t),n.getDate()<e.getDate()?(n.setDate(0),n):n)}const Y=/^\[(.*)\]$/,$=(e,t)=>(e.F=Math.trunc(1e6*t),e),H=(e,t)=>(e.f=Math.trunc(1e3*t),$(e,1e3*Math.abs(t)%1/1e3)),Z=(e,t)=>(e.S=Math.trunc(t),H(e,Math.abs(t)%1)),_=(e,t)=>(e.s=Math.trunc(t/1e3),Z(e,Math.abs(t)%1e3)),k=(e,t)=>(e.m=Math.trunc(t/6e4),_(e,Math.abs(t)%6e4)),j=(e,t)=>(e.H=Math.trunc(t/36e5),k(e,Math.abs(t)%36e5)),z=(e,t,r=f)=>{const n=o(t).slice(1);return n.reduce((t,n)=>t+(t=>{if(t[0]in e){const n=e[t[0]]??0;return r.encode(`${(e=>e<0||0===e&&1/e==-1/0?"-":"")(n)}${String(Math.abs(n)).padStart(t.length,"0")}`)}return Y.test(t)?t.replace(Y,"$1"):t})(n),"")};class F{constructor(e){this.time=e}toNanoseconds(){return{value:1e6*this.time,format:(e,t)=>z($({},this.time),e,t),toParts:()=>({nanoseconds:Math.trunc(1e6*this.time)+0})}}toMicroseconds(){return{value:1e3*this.time,format:(e,t)=>z(H({},this.time),e,t),toParts:()=>({microseconds:Math.trunc(1e3*this.time)+0,nanoseconds:Math.trunc(1e6*this.time%1e3)+0})}}toMilliseconds(){return{value:this.time,format:(e,t)=>z(Z({},this.time),e,t),toParts:()=>({milliseconds:Math.trunc(this.time)+0,microseconds:Math.trunc(1e3*this.time%1e3)+0,nanoseconds:Math.trunc(1e6*this.time%1e3)+0})}}toSeconds(){return{value:this.time/1e3,format:(e,t)=>z(_({},this.time),e,t),toParts:()=>({seconds:Math.trunc(this.time/1e3)+0,milliseconds:Math.trunc(this.time%1e3)+0,microseconds:Math.trunc(1e3*this.time%1e3)+0,nanoseconds:Math.trunc(1e6*this.time%1e3)+0})}}toMinutes(){return{value:this.time/6e4,format:(e,t)=>z(k({},this.time),e,t),toParts:()=>({minutes:Math.trunc(this.time/6e4)+0,seconds:Math.trunc(this.time%864e5%36e5%6e4/1e3)+0,milliseconds:Math.trunc(this.time%1e3)+0,microseconds:Math.trunc(1e3*this.time%1e3)+0,nanoseconds:Math.trunc(1e6*this.time%1e3)+0})}}toHours(){return{value:this.time/36e5,format:(e,t)=>z(j({},this.time),e,t),toParts:()=>({hours:Math.trunc(this.time/36e5)+0,minutes:Math.trunc(this.time%864e5%36e5/6e4)+0,seconds:Math.trunc(this.time%864e5%36e5%6e4/1e3)+0,milliseconds:Math.trunc(this.time%1e3)+0,microseconds:Math.trunc(1e3*this.time%1e3)+0,nanoseconds:Math.trunc(1e6*this.time%1e3)+0})}}toDays(){return{value:this.time/864e5,format:(e,t)=>{return z((r={},n=this.time,r.D=Math.trunc(n/864e5),j(r,Math.abs(n)%864e5)),e,t);var r,n},toParts:()=>({days:Math.trunc(this.time/864e5)+0,hours:Math.trunc(this.time%864e5/36e5)+0,minutes:Math.trunc(this.time%864e5%36e5/6e4)+0,seconds:Math.trunc(this.time%864e5%36e5%6e4/1e3)+0,milliseconds:Math.trunc(this.time%1e3)+0,microseconds:Math.trunc(1e3*this.time%1e3)+0,nanoseconds:Math.trunc(1e6*this.time%1e3)+0})}}}exports.Duration=F,exports.addDays=function(e,t,r){if(p(r)){const n=c(e,r.zone_name);return n.day+=t,n.timezoneOffset=0,S(u(n),r)}const n=new Date(e.getTime());return y(r)?(n.setUTCDate(n.getUTCDate()+t),n):(n.setDate(n.getDate()+t),n)},exports.addHours=function(e,t){return new Date(e.getTime()+36e5*t)},exports.addMilliseconds=function(e,t){return new Date(e.getTime()+t)},exports.addMinutes=function(e,t){return new Date(e.getTime()+6e4*t)},exports.addMonths=U,exports.addSeconds=function(e,t){return new Date(e.getTime()+1e3*t)},exports.addYears=function(e,t,r){return U(e,12*t,r)},exports.compile=o,exports.format=T,exports.isLeapYear=e=>!((e%4||!(e%100))&&e%400),exports.isSameDay=(e,t)=>e.toDateString()===t.toDateString(),exports.isValid=function(e,t,r){return v(x(e,t,r),r)},exports.parse=A,exports.preparse=x,exports.subtract=(e,t)=>new F(t.getTime()-e.getTime()),exports.transform=function(e,t,r,n,s){return T(A(e,t,n),r,s)};