date-format-lite
Version:
A small library for parsing and formatting dates
8 lines (7 loc) • 2.8 kB
JavaScript
/*! litejs.com/MIT-LICENSE.txt */
!function(f,h){var n=/("|')((?:\\?.)*?)\1|([YMDZ])\3\3\3?|([YMDHhmsWSZ])(\4?)|[uUASwoQ]/g,p=/(\d+)[-.\/](\d+)[-.\/](\d+)/,q=/(\d+):(\d+)(?::(\d+))?(\.\d+)?(?:\s*(?:(a)|(p))\.?m\.?)?(\s*(?:Z|GMT|UTC)?(?:([-+]\d\d):?(\d\d)?)?)?/i,r=/\\(.)/g,t={D:"Date",h:"Hours",m:"Minutes",s:"Seconds",S:"Milliseconds"},l={seconds:1E3,sec:1E3,minutes:6E4,min:6E4,hours:36E5,hour:36E5,days:864E5,day:864E5,weeks:6048E5,week:6048E5};f[h].date=f[h].format=function(a,d){a=f.masks[a]||a||f.masks["default"];var e=this,b=+e,
c="get"+("UTC:"==a.slice(0,4)?(a=a.slice(4),"UTC"):""),g=void 0==d?e._z:d;if(void 0!=g&&"get"==c){c="getUTC";e.setTime(b+36E5*g);var h=60*g}a=a.replace(n,function(a,k,d,g,m,l){d="Y"==g?e[c+"FullYear"]():"Z"==m||"Z"==g?(k=h||"get"==c&&-e.getTimezoneOffset()||0,k?(0>k?(k=-k,"-"):"+")+(600>k?"0":"")+(0|k/60)+((k%=60)||g?(l||"ZZZZ"==a?"":":")+(9<k?k:"0"+k):""):"Z"):g?f.names[e[c+("M"==g?"Month":"Day")]()+("DDD"==a?24:"D"==g?31:"MMM"==a?0:12)]:"Y"==m?e[c+"FullYear"]()%100:"W"==m?(k=new f(b+864E5*(4-(e[c+
"Day"]()||7))),Math.ceil(((k.getTime()-k["s"+c.slice(1)+"Month"](0,1))/864E5+1)/7)):"M"==m?e[c+"Month"]()+1:"H"==m?e[c+"Hours"]()%12||12:m?e[c+t[m]]():"u"==a?e/1E3>>>0:"U"==a?b:"Q"==a?(e[c+"Month"]()/3|0)+1:"A"==a?f[11<e[c+"Hours"]()?"pm":"am"]:"w"==a?e[c+"Day"]()||7:"o"==a?(new f(b+864E5*(4-(e[c+"Day"]()||7))))[c+"FullYear"]():k?d.replace(r,"$1"):a;"SS"==a&&100>d&&(d="0"+d);return l&&10>d&&"Z"!=m?"0"+d:d});void 0!=h&&e.setTime(b);return a};f[h].tz=function(a){this._z=a;return this};f[h].add=function(a,
d){a|=0;"month"==d||"months"==d||("year"==d||"years"==d)&&(a*=12)?this.setUTCMonth(this.getUTCMonth()+a):a&&this.setTime(this.getTime()+a*(l[d]||1));return this};f[h].startOf=function(a){if("year"==a||"years"==a)this.setUTCMonth(0,1),a="day";else if("month"==a||"months"==a)this.setUTCDate(1),a="day";this.setTime(this-this%(l[a]||1));return this};f[h].endOf=function(a){return this.startOf(a).add(1,a).add(-1)};f[h].diff=function(a,d){return(this-a)/(l[d]||1)|0};f.am="AM";f.pm="PM";f.masks={"default":"DDD MMM DD YYYY hh:mm:ss",
iso:"UTC:YYYY-MM-DD'T'hh:mm:ss'Z'"};f.names="JanFebMarAprMayJunJulAugSepOctNovDecJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecemberSunMonTueWedThuFriSatSundayMondayTuesdayWednesdayThursdayFridaySaturday".match(/.[a-z]+/g);String[h].date=Number[h].date=function(a,d,e){var b,c=new f,g=+this||""+this;if(isNaN(g)){if(b=g.match(p)){var h=99<b[1]?1:3;var l=f.middleEndian?4-h:2;c.setFullYear(b[h],b[l]-1,b[6-l-h])}b=g.match(q)||[0,0,0];c.setHours(b[6]&&12>b[1]?+b[1]+12:b[5]&&12==b[1]?
0:b[1],b[2],b[3]|0,1E3*b[4]|0);b[7]&&(e=(b[8]|0)+(b[9]|0)/(0>b[8]?-60:60))}else c.setTime(4294967296>g?1E3*g:g);void 0!=e&&c.setTime(c-6E4*(60*e+c.getTimezoneOffset()));return a?c.format(a,d):c}}(Date,"prototype");