-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchronos.min.js
More file actions
1 lines (1 loc) · 12.9 KB
/
Copy pathchronos.min.js
File metadata and controls
1 lines (1 loc) · 12.9 KB
1
!function(e){"use strict";class Chronos{constructor(e,t="UTC"){this._date=this._parseInput(e),this._timezone=t,this._locale="en-US"}_parseInput(e){if(e instanceof Date)return new Date(Date.UTC(e.getUTCFullYear(),e.getUTCMonth(),e.getUTCDate(),e.getUTCHours(),e.getUTCMinutes(),e.getUTCSeconds(),e.getUTCMilliseconds()));if(e instanceof Chronos)return new Date(e._date);if("string"==typeof e)return new Date(e);if("number"==typeof e)return new Date(e);if(void 0===e)return new Date;throw new Error("Invalid input type for Chronos")}_getDateInTimezone(){return new Date(this._date.toLocaleString("en-US",{timeZone:this._timezone}))}_createNewChronos(e,t,s,r=0,n=0,a=0,o=0){return new Chronos(Date.UTC(e,t,s,r,n,a,o),this._timezone)}year(){return this._getDateInTimezone().getFullYear()}month(){return this._getDateInTimezone().getMonth()+1}date(){return this._getDateInTimezone().getDate()}day(){return this._getDateInTimezone().getDay()}hours(){return this._getDateInTimezone().getHours()}minutes(){return this._getDateInTimezone().getMinutes()}seconds(){return this._getDateInTimezone().getSeconds()}milliseconds(){return this._date.getMilliseconds()}timestamp(){return Math.floor(this._date.getTime()/1e3)}add(e,t){const s=new Date(this._date);switch(t){case"years":case"year":case"y":s.setUTCFullYear(s.getUTCFullYear()+e);break;case"months":case"month":case"M":s.setUTCMonth(s.getUTCMonth()+e);break;case"weeks":case"week":case"w":s.setUTCDate(s.getUTCDate()+7*e);break;case"days":case"day":case"d":s.setUTCDate(s.getUTCDate()+e);break;case"hours":case"hour":case"h":s.setUTCHours(s.getUTCHours()+e);break;case"minutes":case"minute":case"m":s.setUTCMinutes(s.getUTCMinutes()+e);break;case"seconds":case"second":case"s":s.setUTCSeconds(s.getUTCSeconds()+e);break;default:throw new Error("Invalid unit: "+t)}return new Chronos(s,this._timezone)}age(e=new Date){const t=this._date;let s=e.getFullYear()-t.getFullYear();const r=e.getMonth()-t.getMonth();return(r<0||0===r&&e.getDate()<t.getDate())&&s--,s}subtract(e,t){return this.add(-e,t)}diffForHumans(e=null,t={}){const{absolute:s=!1,short:r=!1,parts:n=1,threshold:a=.9}=t,o=e?Chronos.parse(e):Chronos.now(),i=Math.abs(this.valueOf()-o.valueOf()),h=this.valueOf()>o.valueOf(),u=[{name:"year",seconds:31536e3,short:"y"},{name:"month",seconds:2592e3,short:"mo"},{name:"week",seconds:604800,short:"w"},{name:"day",seconds:86400,short:"d"},{name:"hour",seconds:3600,short:"h"},{name:"minute",seconds:60,short:"m"},{name:"second",seconds:1,short:"s"}];let d=[],c=i/1e3;for(const e of u){const t=Math.floor(c/e.seconds);if(t>=a||d.length>0){const s=1===t?e.name:`${e.name}s`,n=r?e.short:s;d.push(`${t} ${n}`),c%=e.seconds}if(d.length===n)break}if(0===d.length)return"just now";let m=d.join(r?" ":", ");return s||(m=h?`in ${m}`:`${m} ago`),m}addYears(e){return this.add(e,"years")}addMonths(e){return this.add(e,"months")}addDays(e){return this.add(e,"days")}addHours(e){return this.add(e,"hours")}addMinutes(e){return this.add(e,"minutes")}addSeconds(e){return this.add(e,"seconds")}subYears(e){return this.subtract(e,"years")}subMonths(e){return this.subtract(e,"months")}subDays(e){return this.subtract(e,"days")}subHours(e){return this.subtract(e,"hours")}subMinutes(e){return this.subtract(e,"minutes")}subSeconds(e){return this.subtract(e,"seconds")}format(e){const t=new Date(this._date.toLocaleString("en-US",{timeZone:this._timezone})),s=t.getFullYear(),r=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),a=String(t.getHours()).padStart(2,"0"),o=String(t.getMinutes()).padStart(2,"0"),i=String(t.getSeconds()).padStart(2,"0");return e.replace("YYYY",s).replace("MM",r).replace("DD",n).replace("HH",a).replace("mm",o).replace("ss",i)}getMonthName(){return["January","February","March","April","May","June","July","August","September","October","November","December"][this._date.getMonth()]}getDayName(){return["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this._date.getDay()]}isBefore(e){return this._date<Chronos.parse(e)._date}isAfter(e){const t=e instanceof Chronos?e:new Chronos(e);return this.valueOf()>t.valueOf()}isSame(e){const t=e instanceof Chronos?e:new Chronos(e);return this.valueOf()===t.valueOf()}isBetween(e,t){const s=Chronos.parse(e)._date,r=Chronos.parse(t)._date;return this._date>=s&&this._date<=r}isSameDay(e){const t=Chronos.parse(e);return this.year()===t.year()&&this.month()===t.month()&&this.date()===t.date()}isSameMonth(e){const t=Chronos.parse(e);return this.year()===t.year()&&this.month()===t.month()}isSameYear(e){return this.year()===Chronos.parse(e).year()}isWeekday(){const e=this.day();return e>=1&&e<=5}isWeekend(){const e=this.day();return 0===e||6===e}isToday(){return this.isSameDay(new Date)}isTomorrow(){const e=new Date;return e.setDate(e.getDate()+1),this.isSameDay(e)}isYesterday(){const e=new Date;return e.setDate(e.getDate()-1),this.isSameDay(e)}isFuture(){return this._date>new Date}isPast(){return this._date<new Date}isLeapYear(){const e=this.year();return e%4==0&&e%100!=0||e%400==0}quarter(){return Math.floor((this.month()-1)/3)+1}toDate(){return new Date(this._date)}valueOf(){return this._date.getTime()}toString(){return this._date.toISOString()}toJSON(){return this._date.toJSON()}toArray(){const e=this._getDateInTimezone();return[e.getFullYear(),e.getMonth()+1,e.getDate(),e.getHours(),e.getMinutes(),e.getSeconds(),e.getMilliseconds()]}toObject(){const e=this._getDateInTimezone();return{year:e.getFullYear(),month:e.getMonth()+1,day:e.getDate(),dayOfWeek:e.getDay(),hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds(),millisecond:e.getMilliseconds(),timestamp:Math.floor(e.getTime()/1e3),timezone:this._timezone}}firstOfMonth(){return this._createNewChronos(this.year(),this.month()-1,1,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}lastOfMonth(){return this._createNewChronos(this.year(),this.month(),0,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}nthOfMonth(e,t){let s=t-this.firstOfMonth().day();s<0&&(s+=7);const r=1+s+7*(e-1);return this._createNewChronos(this.year(),this.month()-1,r,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}firstOfQuarter(){const e=3*Math.floor((this.month()-1)/3);return this._createNewChronos(this.year(),e,1,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}lastOfQuarter(){const e=3*Math.floor((this.month()-1)/3);return this._createNewChronos(this.year(),e+3,0,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}nthOfQuarter(e,t){const s=this.firstOfQuarter();let r=t-s.day();r<0&&(r+=7);const n=1+r+7*(e-1);return this._createNewChronos(s.year(),s.month()-1,n,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}firstOfYear(){return this._createNewChronos(this.year(),0,1,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}lastOfYear(){return this._createNewChronos(this.year(),11,31,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}nthOfYear(e,t){let s=t-this.firstOfYear().day();s<0&&(s+=7);const r=1+s+7*(e-1);return this._createNewChronos(this.year(),0,r,this.hours(),this.minutes(),this.seconds(),this.milliseconds())}static now(e="UTC"){return new Chronos(new Date,e)}static parse(e,t="UTC"){return new Chronos(e,t)}static closest(e){if(!Array.isArray(e)||0===e.length)throw new Error("Input must be a non-empty array of dates");const t=Chronos.now();let s=Chronos.parse(e[0]),r=Math.abs(t.valueOf()-s.valueOf());for(let n=1;n<e.length;n++){const a=Chronos.parse(e[n]),o=Math.abs(t.valueOf()-a.valueOf());o<r&&(s=a,r=o)}return s}static farthest(e){if(!Array.isArray(e)||0===e.length)throw new Error("Input must be a non-empty array of dates");const t=Chronos.now();let s=Chronos.parse(e[0]),r=Math.abs(t.valueOf()-s.valueOf());for(let n=1;n<e.length;n++){const a=Chronos.parse(e[n]),o=Math.abs(t.valueOf()-a.valueOf());o>r&&(s=a,r=o)}return s}static today(){const e=new Date;return new Chronos(new Date(e.getFullYear(),e.getMonth(),e.getDate()))}static tomorrow(){const e=new Date;return e.setDate(e.getDate()+1),new Chronos(new Date(e.getFullYear(),e.getMonth(),e.getDate()))}static yesterday(){const e=new Date;return e.setDate(e.getDate()-1),new Chronos(new Date(e.getFullYear(),e.getMonth(),e.getDate()))}setLocale(e){if("string"!=typeof e)throw new Error("Locale must be a string");try{return new Intl.DateTimeFormat(e),this._locale=e,this}catch(e){throw new Error("Invalid locale")}}next(e){return this._moveToDayOfWeek(e,1)}previous(e){return this._moveToDayOfWeek(e,-1)}_moveToDayOfWeek(e,t){const s=["sunday","monday","tuesday","wednesday","thursday","friday","saturday"].indexOf(e.toLowerCase());if(-1===s)throw new Error("Invalid day of week");let r=(s-this._date.getDay()+7)%7;0===r&&(r=7),-1===t&&(r=7===r?0:7-r);const n=new Date(this._date);return n.setDate(n.getDate()+r*t),new Chronos(n,this._timezone)}static createFromFormat(e,t){const s={YYYY:"(\\d{4})",YY:"(\\d{2})",MM:"(\\d{2})",M:"(\\d{1,2})",DD:"(\\d{2})",D:"(\\d{1,2})",HH:"(\\d{2})",H:"(\\d{1,2})",hh:"(\\d{2})",h:"(\\d{1,2})",mm:"(\\d{2})",m:"(\\d{1,2})",ss:"(\\d{2})",s:"(\\d{1,2})",SSS:"(\\d{3})",S:"(\\d{1,3})",A:"(AM|PM)",a:"(am|pm)"};let r=e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");const n=e.match(/\w+/g)||[],a={};n.forEach((e=>{s[e]&&(r=r.replace(e,s[e]))}));const o=new RegExp(`^${r}$`),i=t.match(o);if(!i)throw new Error("Invalid date string for the given format");i.shift(),n.forEach(((e,t)=>{a[e]=i[t]}));let h=a.YYYY?parseInt(a.YYYY):a.YY?parseInt(a.YY)+2e3:(new Date).getFullYear();const u=parseInt(a.MM||a.M||1)-1,d=parseInt(a.DD||a.D||1);let c=parseInt(a.HH||a.H||a.hh||a.h||0);const m=parseInt(a.mm||a.m||0),l=parseInt(a.ss||a.s||0),g=parseInt(a.SSS||a.S||0);("PM"===a.A||"pm"===a.a)&&c<12?c+=12:"AM"!==a.A&&"am"!==a.a||12!==c||(c=0);const w=new Chronos(new Date(h,u,d,c,m,l,g));return w.setLocale(locale),w}static createFromTimestamp(e){if("number"!=typeof e||isNaN(e))throw new Error("Invalid timestamp");return new Chronos(new Date(1e3*e))}static createFromTimestampMs(e){if("number"!=typeof e||isNaN(e))throw new Error("Invalid timestamp");return new Chronos(new Date(e))}static makeDateTimeString(e){return new Chronos(e).format("YYYY-MM-DD HH:mm:ss")}setTimezone(e){if("string"!=typeof e)throw new Error("Timezone must be a string");try{return Intl.DateTimeFormat("en-US",{timeZone:e}),this._timezone=e,this}catch(e){throw new Error("Invalid timezone")}}translatedFormat(e,t={}){const s=this._getDateInTimezone(),r=s.getFullYear(),n=s.getMonth(),a=s.getDate(),o=s.getDay(),i=s.getHours(),h=s.getMinutes(),u=s.getSeconds(),d={months:["January","February","March","April","May","June","July","August","September","October","November","December"],monthsShort:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],weekdays:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],weekdaysShort:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],meridiem:(e,t,s)=>{const r=e>11?"PM":"AM";return s?r.toLowerCase():r},...t};return e.replace(/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}/g,(e=>{if("["===e[0]&&"]"===e[e.length-1])return e.slice(1,-1);switch(e){case"YYYY":return r;case"YY":return String(r).slice(-2);case"MMMM":return d.months[n];case"MMM":return d.monthsShort[n];case"MM":return String(n+1).padStart(2,"0");case"M":return String(n+1);case"DD":return String(a).padStart(2,"0");case"D":return String(a);case"dddd":return d.weekdays[o];case"ddd":return d.weekdaysShort[o];case"HH":return String(i).padStart(2,"0");case"H":return String(i);case"hh":return String(i%12||12).padStart(2,"0");case"h":return String(i%12||12);case"mm":return String(h).padStart(2,"0");case"m":return String(h);case"ss":return String(u).padStart(2,"0");case"s":return String(u);case"a":return d.meridiem(i,h,!0);case"A":return d.meridiem(i,h,!1);default:return e}}))}duration(e){const t=Math.abs(this.valueOf()-Chronos.parse(e).valueOf()),s=Math.floor(t/1e3),r=Math.floor(s/60),n=Math.floor(r/60);return{milliseconds:t,seconds:s,minutes:r,hours:n,days:Math.floor(n/24)}}addBusinessDays(e){let t=new Date(this._date),s=0;for(;s<e;)t.setDate(t.getDate()+1),0!==t.getDay()&&6!==t.getDay()&&s++;return new Chronos(t,this._timezone)}static generateCalendar(e,t){const s=new Chronos(new Date(e,t-1,1)),r=new Chronos(new Date(e,t,0)),n=[];for(let e=0;e<s.day();e++)n.push(null);for(let s=1;s<=r.date();s++)n.push(new Chronos(new Date(e,t-1,s)));for(;n.length%7!=0;)n.push(null);return n}fromNow(){const e=Chronos.now().setTimezone(this._timezone);return this.diffForHumans(e)}startOf(e){const t=new Date(this._date);switch(e){case"year":t.setMonth(0,1),t.setHours(0,0,0,0);break;case"month":t.setDate(1),t.setHours(0,0,0,0);break;case"day":t.setHours(0,0,0,0);break;case"hour":t.setMinutes(0,0,0);break;default:throw new Error("Invalid unit")}return new Chronos(t,this._timezone)}endOf(e){const t=new Date(this._date);switch(e){case"year":t.setMonth(11,31),t.setHours(23,59,59,999);break;case"month":t.setMonth(t.getMonth()+1,0),t.setHours(23,59,59,999);break;case"day":t.setHours(23,59,59,999);break;case"hour":t.setMinutes(59,59,999);break;default:throw new Error("Invalid unit")}return new Chronos(t,this._timezone)}}e.Chronos=Chronos}("undefined"!=typeof window?window:this);