UNPKG

carbonjs

Version:

Javascript date library alternative to Moment.js with the same modern API

1 lines 6.6 kB
"use strict";const e=require("./constants"),t=require("./utils");let s="en";const r={en:require("./locales/en")},a=[];class n{constructor(e,t,r){this._localeName=s,e=this._parseDate(e,t,r),this._date=e,this._year=e.getFullYear(),this._month=e.getMonth(),this._weekday=e.getDay(),this._day=e.getDate(),this._hours=e.getHours(),this._minutes=e.getMinutes(),this._seconds=e.getSeconds(),this._milliseconds=e.getMilliseconds()}get _locale(){return r[this._localeName]}_parseDate(s,r,a){if(n.isCarbon(s))return this._localeName=s._localeName,s.toDate();if(null===s)return t.newDate(NaN);if(void 0===s)return t.newDate();if("string"==typeof s){if(r){a&&(this._localeName=n.locale(a,!1));const i=[0,0,0,0,0,0,0],h={},c=/^[,<>./?'";:\\|\[\]{}=+\-_()*&^%$#@!`~ ]+/;let o;for(;o=e.PARSE_REGEX.exec(r);){const e=o[0];if(/^\[.*\]$/.test(e)){s=s.slice(e.length-1).replace(c,"");continue}const t=/[^,<>./?'";:\\|\[\]{}=+\-_()*&^%$#@!`~ ]+/.exec(s)[0];h[e]=t,s=s.slice(t.length).replace(c,"")}return Object.keys(h).forEach(e=>this._parseToken(e,h[e],i,h)),t.newDate(...i)}if(/.*[^Z]$/i.test(s)){const r=s.match(e.REGEX_PARSE);if(r)return t.newDate(r[1],r[2]-1,r[3]||1,r[5]||0,r[6]||0,r[7]||0,r[8]||0)}}return t.newDate(s)}_parseToken(e,s,r,a){switch(e){case"YY":r[0]=+(`${t.newDate().getFullYear()}`.slice(0,2)+s);break;case"YYYY":r[0]=+s;break;case"M":case"MM":r[1]=+s-1;break;case"MMM":const n=this._locale;r[1]=t.findShortIndex(n.monthsShort,s,n.months);break;case"MMMM":r[1]=this._locale.months.indexOf(s);break;case"D":case"DD":r[2]=+s;break;case"H":case"HH":r[3]=+s;break;case"h":case"hh":const i=(a.A||a.a||"am").toLowerCase();r[3]=+s+("am"===i?0:12);break;case"m":case"mm":r[4]=+s;break;case"s":case"ss":r[5]=+s;break;case"S":case"SS":case"SSS":r[6]=+s}}_clone(e){const t=new n(e);return t._localeName=this._localeName,t}_compare(e){return this.valueOf()-new n(e).valueOf()}_edge(s,r=!0){const a=(s,a)=>{const n=this._clone(t.newDate(this._year,a,s));return r?n:n.endOf(e.DAY)},n=(e,t)=>{const s=this.toDate();return this._clone(s[e].apply(s,r?[0,0,0,0].slice(t):[23,59,59,999].slice(t)))};switch(s=t.prettyUnit(s)){case e.YEAR:return r?a(1,0):a(31,11);case e.MONTH:return r?a(1,this._month):a(0,this._month+1);case e.WEEK:return a(r?this._day-this._weekday:this._day+(6-this._weekday),this._month);case e.DAY:return n("setHours",0);case e.HOUR:return n("setMinutes",1);case e.MINUTE:return n("setSeconds",2);case e.SECOND:return n("setMilliseconds",3);default:return this.clone()}}locale(e){const t=this.clone();return t._localeName=n.locale(e,!1),t}isValid(){return"Invalid Date"!==this._date.toString()}isSame(e){return 0===this._compare(e)}isBefore(e){return this._compare(e)<0}isAfter(e){return this._compare(e)>0}year(){return this._year}month(){return this._month}weekday(){return this._weekday}day(){return this._day}hour(){return this._hours}minute(){return this._minutes}second(){return this._seconds}millisecond(){return this._milliseconds}utcOffset(){return this._date.getTimezoneOffset()}valueOf(){return this._date.getTime()}startOf(e){return this._edge(e)}endOf(e){return this._edge(e,!1)}daysInMonth(){return this.endOf(e.MONTH).day()}unix(){return Math.floor(this.valueOf()/1e3)}set(s,r){s=t.prettyUnit(s);const a=t.newDate(this._date);switch(s){case e.YEAR:a.setFullYear(r);break;case e.MONTH:a.setMonth(r);break;case e.DAY:a.setDate(r);break;case e.WEEKDAY:a.setDate(this._day+(r-this._weekday));break;case e.HOUR:a.setHours(r);break;case e.MINUTE:a.setMinutes(r);break;case e.SECOND:a.setSeconds(r);break;case e.MILLISECOND:a.setMilliseconds(r)}return this._clone(a)}add(s,r){s=+s;const a=(t,r)=>{const a=this.set(e.DAY,1).set(t,s+r);return a.set(e.DAY,Math.min(this._day,a.daysInMonth()))};if((r=t.prettyUnit(r))===e.YEAR)return a(r,this._year);if(r===e.MONTH)return a(r,this._month);let n;switch(r){case e.WEEK:s*=7,n=this._day,r="day";break;case e.DAY:r="day",n=this._day;break;case e.HOUR:n=this._hours;break;case e.MINUTE:n=this._minutes;break;case e.SECOND:n=this._seconds;break;default:n=this._milliseconds}return this.set(r,n+s)}subtract(e,t){return this.add(-e,t)}format(s=e.FORMAT_DEFAULT){const r=t.padZoneStr(this._date.getTimezoneOffset()),a=this._locale,{weekdays:n,months:i}=a;return s.replace(e.REGEX_FORMAT,e=>{if(e.indexOf("[")>-1)return e.replace(/\[|\]/g,"");switch(e){case"YY":return`${this._year}`.slice(-2);case"YYYY":return`${this._year}`;case"M":return`${this._month+1}`;case"MM":return t.padStart(this._month+1,2,"0");case"MMM":return t.getShort(a.monthsShort,this._month,i,3);case"MMMM":return i[this._month];case"D":return`${this._day}`;case"DD":return t.padStart(this._day,2,"0");case"d":return`${this._weekday}`;case"dd":return t.getShort(a.weekdaysMin,this._weekday,n,2);case"ddd":return t.getShort(a.weekdaysShort,this._weekday,n,3);case"dddd":return n[this._weekday];case"H":return`${this._hours}`;case"HH":return t.padStart(this._hours,2,"0");case"h":case"hh":return 0===this._hours?12:t.padStart(this._hours<13?this._hours:this._hours-12,"hh"===e?2:1,"0");case"a":return this._hours<12?"am":"pm";case"A":return this._hours<12?"AM":"PM";case"m":return`${this._minutes}`;case"mm":return t.padStart(this._minutes,2,"0");case"s":return`${this._seconds}`;case"ss":return t.padStart(this._seconds,2,"0");case"SSS":return t.padStart(this._milliseconds,3,"0");case"Z":return r;default:return r.replace(":","")}})}diff(s,r,a=!1){s=this._clone(s),r=t.prettyUnit(r);const n=this.valueOf()-s.valueOf(),i=6e4*(this.utcOffset()-s.utcOffset());let h=t.monthDiff(this,s);switch(r){case e.YEAR:h/=12;break;case e.MONTH:break;case e.QUARTER:h/=3;break;case e.WEEK:h=(n-i)/e.MILLISECONDS_A_WEEK;break;case e.DAY:h=(n-i)/e.MILLISECONDS_A_DAY;break;case e.HOUR:h=n/e.MILLISECONDS_A_HOUR;break;case e.MINUTE:h=n/e.MILLISECONDS_A_MINUTE;break;case e.SECOND:h=n/e.MILLISECONDS_A_SECOND;break;default:h=n}return a?h:t.absFloor(h)}clone(){return this._clone(this)}toDate(){return t.newDate(this._date)}toArray(){return[this._year,this._month,this._day,this._hours,this._minutes,this._seconds,this._milliseconds]}toObject(){return{years:this._year,months:this._month,date:this._day,hours:this._hours,minutes:this._minutes,seconds:this._seconds,milliseconds:this._milliseconds}}toISOString(){return this.toDate().toISOString()}toJSON(){return this.toISOString()}toString(){return this._date.toUTCString()}}n._en=r.en,n.isCarbon=(e=>e instanceof n),n.extend=((e,t)=>{const s=e.name;a.indexOf(s)>-1||(e(n,t),a.push(s))}),n.locale=((e,t=!0)=>{let a=s;if("string"==typeof e)r[e]&&(a=e);else{const t=e.name;r[t]=e,a=t}return t&&(s=a),a}),n.parse=((e,t,s)=>new n(e,t,s)),module.exports=n;