date-and-time
Version:
The simplest, most intuitive date and time library
3 lines (2 loc) • 13.2 kB
JavaScript
/** @preserve Copyright (c) KNOWLEDGECODE - MIT License */
const e=e=>[e,...e.match(/\[(?:[^[\]]|\[[^[\]]*])*]|([A-Za-z])\1*|\.{3}|./g)||[]],t=new Map,n=e=>t.get(e)||(()=>{const n=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&&t.set(e,n),n})(),r=(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=n(t).formatToParts(e).reduce((e,{type:t,value:n})=>{switch(t){case"weekday":e[t]="SunMonTueWedThuFriSat".indexOf(n)/3;break;case"hour":e[t]=+n%24;break;case"year":case"month":case"day":case"minute":case"second":case"fractionalSecond":e[t]=+n}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},s=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 o{constructor(e,t){this.parts=r(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 a{}const i=(e,t)=>e.getFullYear()+("buddhist"===t?543:0);const c=new class extends a{YYYY(e,t){return`000${i(e,t.calendar)}`.slice(-4)}YY(e,t){return`0${i(e,t.calendar)}`.slice(-2)}Y(e,t){return`${i(e,t.calendar)}`}MMMM(e,t,n){return t.locale.getMonthList({style:"long",compiledObj:n})[e.getMonth()]||""}MMM(e,t,n){return t.locale.getMonthList({style:"short",compiledObj:n})[e.getMonth()]||""}MM(e){return`0${e.getMonth()+1}`.slice(-2)}M(e){return`${e.getMonth()+1}`}DD(e){return`0${e.getDate()}`.slice(-2)}D(e){return`${e.getDate()}`}HH(e,t){return`0${e.getHours()||("h24"===t.hour24?24:0)}`.slice(-2)}H(e,t){return`${e.getHours()||("h24"===t.hour24?24:0)}`}AA(e,t,n){return t.locale.getMeridiemList({style:"long",compiledObj:n,case:"uppercase"})[+(e.getHours()>11)]||""}A(e,t,n){return t.locale.getMeridiemList({style:"short",compiledObj:n,case:"uppercase"})[+(e.getHours()>11)]||""}aa(e,t,n){return t.locale.getMeridiemList({style:"long",compiledObj:n,case:"lowercase"})[+(e.getHours()>11)]||""}a(e,t,n){return t.locale.getMeridiemList({style:"short",compiledObj:n,case:"lowercase"})[+(e.getHours()>11)]||""}hh(e,t){return`0${e.getHours()%12||("h12"===t.hour12?12:0)}`.slice(-2)}h(e,t){return`${e.getHours()%12||("h12"===t.hour12?12:0)}`}mm(e){return`0${e.getMinutes()}`.slice(-2)}m(e){return`${e.getMinutes()}`}ss(e){return`0${e.getSeconds()}`.slice(-2)}s(e){return`${e.getSeconds()}`}SSS(e){return`00${e.getMilliseconds()}`.slice(-3)}SS(e){return`00${e.getMilliseconds()}`.slice(-3,-1)}S(e){return`00${e.getMilliseconds()}`.slice(-3,-2)}dddd(e,t,n){return t.locale.getDayOfWeekList({style:"long",compiledObj:n})[e.getDay()]||""}ddd(e,t,n){return t.locale.getDayOfWeekList({style:"short",compiledObj:n})[e.getDay()]||""}dd(e,t,n){return t.locale.getDayOfWeekList({style:"narrow",compiledObj:n})[e.getDay()]||""}Z(e){const t=e.getTimezoneOffset(),n=Math.abs(t);return`${t>0?"-":"+"}${("0"+(n/60|0)).slice(-2)}${("0"+n%60).slice(-2)}`}ZZ(e){const t=e.getTimezoneOffset(),n=Math.abs(t);return`${t>0?"-":"+"}${("0"+(n/60|0)).slice(-2)}:${("0"+n%60).slice(-2)}`}},u={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 l=new class{getLocale(){return"en"}getMonthList(e){return"long"===e.style?u.MMMM:u.MMM}getDayOfWeekList(e){return"long"===e.style?u.dddd:"short"===e.style?u.ddd:u.dd}getMeridiemList(e){return"long"===e.style?"lowercase"===e.case?u.aa:u.AA:"lowercase"===e.case?u.a:u.A}},h={encode:e=>e,decode:e=>e};const d=e=>"object"==typeof e&&"zone_name"in e&&"gmt_offset"in e,m=e=>"UTC"===e,g=(e,t)=>{const r=n("UTC"),s=n(t.zone_name),o=t.gmt_offset;for(let t=0;t<2;t++){const n=r.format(e-86400*t*1e3);for(let r=0,a=o.length;r<a;r++)if(s.format(e-1e3*(o[r]+86400*t))===n)return o[r]}return NaN},M=/^\[(.*)\]$/;function f(t,n,r){const s=("string"==typeof n?e(n):n).slice(1),a=d(r?.timeZone)||m(r?.timeZone)?r.timeZone:void 0,i="string"==typeof a?a:a?.zone_name||"",u=i?new o(t,i):t,g={hour12:r?.hour12||"h12",hour24:r?.hour24||"h23",numeral:r?.numeral||h,calendar:r?.calendar||"gregory",timeZone:a,locale:r?.locale||l},f=[...r?.plugins||[],c],y=g.numeral.encode;return s.reduce((e,t)=>e+((e,t)=>{for(const n of f)if(n[e])return y(n[e](u,g,t));return M.test(e)?e.replace(M,"$1"):e})(t,s),"")}class y{}const D=(e,t,n)=>{const r=e.exec(t)?.[0]||"";return{value:+r,length:r.length,token:n}},p=(e,t,n)=>e.reduce((e,r,s)=>r.length>e.length&&!t.indexOf(r)?{value:s,length:r.length,token:n}:e,{value:-1,length:0,token:n});const T=new class extends y{YYYY(e){return D(/^\d{4}/,e,"Y")}Y(e){return D(/^\d{1,4}/,e,"Y")}MMMM(e,t,n){const r=t.locale.getMonthList({style:"long",compiledObj:n}),s=t.locale.getLocale(),o=t.ignoreCase?p(r.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"M"):p(r,e,"M");return o.value++,o}MMM(e,t,n){const r=t.locale.getMonthList({style:"short",compiledObj:n}),s=t.locale.getLocale(),o=t.ignoreCase?p(r.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"M"):p(r,e,"M");return o.value++,o}MM(e){return D(/^\d\d/,e,"M")}M(e){return D(/^\d\d?/,e,"M")}DD(e){return D(/^\d\d/,e,"D")}D(e){return D(/^\d\d?/,e,"D")}HH(e){return D(/^\d\d/,e,"H")}H(e){return D(/^\d\d?/,e,"H")}AA(e,t,n){const r=t.locale.getMeridiemList({style:"long",compiledObj:n,case:"uppercase"}),s=t.locale.getLocale();return t.ignoreCase?p(r.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):p(r,e,"A")}A(e,t,n){const r=t.locale.getMeridiemList({style:"short",compiledObj:n,case:"uppercase"}),s=t.locale.getLocale();return t.ignoreCase?p(r.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):p(r,e,"A")}aa(e,t,n){const r=t.locale.getMeridiemList({style:"long",compiledObj:n,case:"lowercase"}),s=t.locale.getLocale();return t.ignoreCase?p(r.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):p(r,e,"A")}a(e,t,n){const r=t.locale.getMeridiemList({style:"short",compiledObj:n,case:"lowercase"}),s=t.locale.getLocale();return t.ignoreCase?p(r.map(e=>e.toLocaleLowerCase(s)),e.toLocaleLowerCase(s),"A"):p(r,e,"A")}hh(e){return D(/^\d\d/,e,"h")}h(e){return D(/^\d\d?/,e,"h")}mm(e){return D(/^\d\d/,e,"m")}m(e){return D(/^\d\d?/,e,"m")}ss(e){return D(/^\d\d/,e,"s")}s(e){return D(/^\d\d?/,e,"s")}SSS(e){return D(/^\d{1,3}/,e,"S")}SS(e){const t=D(/^\d\d?/,e,"S");return t.value*=10,t}S(e){const t=D(/^\d/,e,"S");return t.value*=100,t}Z(e){const t=D(/^[+-][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)||["","",""],n=+(t[1]+t[2]);return{value:-60*(n/100|0)-n%100,length:t[0].length,token:"Z"}}},L=/^\[(.*)\]$/;function C(t,n,r){const s=("string"==typeof n?e(n):n).slice(1),o={hour12:r?.hour12||"h12",hour24:r?.hour24||"h23",numeral:r?.numeral||h,calendar:r?.calendar||"gregory",ignoreCase:r?.ignoreCase||!1,timeZone:d(r?.timeZone)||m(r?.timeZone)?r.timeZone:void 0,locale:r?.locale||l},a={_index:0,_length:0,_match:0},i=[...r?.plugins||[],T],c=(e,t)=>{for(const n of i)if(n[e])return n[e](t,o,s)};t=o.numeral.decode(t);for(const e of s){const n=t.substring(a._index),r=c(e,n);if(r){if(!r.length)break;r.token&&(a[r.token]=r.value+0),a._index+=r.length,a._match++}else if(e===n[0]||" "===e)a._index++;else{if(!L.test(e)||n.indexOf(e.replace(L,"$1"))){if("..."===e){a._index=t.length;break}break}a._index+=e.length-2}}return a._length=t.length,a}function S(e,t){const n=void 0===e.Y?1970:e.Y-("buddhist"===t?.calendar?543:0),[r,s]="h11"===t?.hour12?[0,11]:[1,12],[o,a]="h24"===t?.hour24?[1,24]:[0,23],i=(e,t,n)=>void 0===e||e>=t&&e<=n;return e._index>0&&e._length>0&&e._index===e._length&&e._match>0&&i(n,1,9999)&&i(e.M,1,12)&&i(e.D,1,(c=n,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,r,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 w(e,t,n){return S(C(e,t,n),n)}function b(e,t,n){const r=C(e,t,n);if(!S(r,n))return new Date(NaN);if(r.Y=r.Y?r.Y-("buddhist"===n?.calendar?543:0):1970,r.M=(r.M||1)-(r.Y<100?22801:1),r.D||=1,r.H=(r.H||0)%24||12*(r.A||0)+(r.h||0)%12,r.m||=0,r.s||=0,r.S||=0,d(n?.timeZone)){const e=Date.UTC(r.Y,r.M,r.D,r.H,r.m,r.s,r.S),t=g(e,n.timeZone);return new Date(e-1e3*t)}return m(n?.timeZone)||"Z"in r?new Date(Date.UTC(r.Y,r.M,r.D,r.H,r.m+(r.Z||0),r.s,r.S)):new Date(r.Y,r.M,r.D,r.H,r.m,r.s,r.S)}function O(e,t,n,r,s){return f(b(e,t,r),n,s)}function $(e,t,n){if(d(n)){const o=r(e,n.zone_name);o.month+=t,o.timezoneOffset=0;const a=new Date(s(o));a.getUTCDate()<o.day&&a.setUTCDate(0);const i=s({...o,year:a.getUTCFullYear(),month:a.getUTCMonth()+1,day:a.getUTCDate()}),c=g(i,n);return new Date(i-1e3*c)}const o=new Date(e.getTime());return m(n)?(o.setUTCMonth(o.getUTCMonth()+t),o.getUTCDate()<e.getUTCDate()?(o.setUTCDate(0),o):o):(o.setMonth(o.getMonth()+t),o.getDate()<e.getDate()?(o.setDate(0),o):o)}function v(e,t,n){return $(e,12*t,n)}function A(e,t,n){if(d(n)){const o=r(e,n.zone_name);o.day+=t,o.timezoneOffset=0;const a=s(o),i=g(a,n);return new Date(a-1e3*i)}const o=new Date(e.getTime());return m(n)?(o.setUTCDate(o.getUTCDate()+t),o):(o.setDate(o.getDate()+t),o)}function U(e,t){return new Date(e.getTime()+36e5*t)}function Y(e,t){return new Date(e.getTime()+6e4*t)}function H(e,t){return new Date(e.getTime()+1e3*t)}function Z(e,t){return new Date(e.getTime()+t)}const _=/^\[(.*)\]$/,k=(e,t)=>(e.F=Math.trunc(1e6*t),e),j=(e,t)=>(e.f=Math.trunc(1e3*t),k(e,1e3*Math.abs(t)%1/1e3)),x=(e,t)=>(e.S=Math.trunc(t),j(e,Math.abs(t)%1)),z=(e,t)=>(e.s=Math.trunc(t/1e3),x(e,Math.abs(t)%1e3)),F=(e,t)=>(e.m=Math.trunc(t/6e4),z(e,Math.abs(t)%6e4)),P=(e,t)=>(e.H=Math.trunc(t/36e5),F(e,Math.abs(t)%36e5)),W=(t,n,r=h)=>{const s=e(n).slice(1);return s.reduce((e,n)=>e+(e=>{if(e[0]in t){const n=t[e[0]]??0;return r.encode(`${(e=>e<0||0===e&&1/e==-1/0?"-":"")(n)}${`${Math.abs(n)}`.padStart(e.length,"0")}`)}return _.test(e)?e.replace(_,"$1"):e})(n),"")};class N{constructor(e){this.time=e}toNanoseconds(){return{value:1e6*this.time,format:(e,t)=>W(k({},this.time),e,t),toParts:()=>({nanoseconds:Math.trunc(1e6*this.time)+0})}}toMicroseconds(){return{value:1e3*this.time,format:(e,t)=>W(j({},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)=>W(x({},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)=>W(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)=>W(F({},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)=>W(P({},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 W((n={},r=this.time,n.D=Math.trunc(r/864e5),P(n,Math.abs(r)%864e5)),e,t);var n,r},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})}}}const J=(e,t)=>new N(t.getTime()-e.getTime()),I=e=>!((e%4||!(e%100))&&e%400),q=(e,t)=>e.toDateString()===t.toDateString();export{N as Duration,A as addDays,U as addHours,Z as addMilliseconds,Y as addMinutes,$ as addMonths,H as addSeconds,v as addYears,e as compile,f as format,I as isLeapYear,q as isSameDay,w as isValid,b as parse,C as preparse,J as subtract,O as transform};