-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdateTime.min.js
26 lines (17 loc) · 4.94 KB
/
dateTime.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// dateTime v2
// author: Bob Gray <[email protected]>
// Adapted from Steven Levithan <stevenlevithan.com> Date Format 1.2.3 (c) 2007-2009
(function(x){if(x.dateTime)return;x.dateTime={
move:function(){var d=x.Date,a=d.prototype,g=a.setUTCDate,h=a.setUTCMonth,i=a.setUTCFullYear,j=a.setUTCMilliseconds,k=a.setUTCSeconds,l=a.setUTCMinutes,m=a.setUTCHours,f=a.getUTCDate,e=a.getUTCMonth,b=a.getUTCFullYear,c=a.getUTCMilliseconds,o=a.getUTCSeconds,p=a.getUTCMinutes,q=a.getUTCHours,r={d:function(n,a){g.call(n,f.call(n)+a)},w:function(a,e){g.call(a,f.call(a)+e*7)},m:function(a,b){h.call(a,e.call(a)+b)},q:function(a,b){h.call(a,e.call(a)+b*3)},y:function(a,e){i.call(a,b.call(a)+e)},h:function(a,e){m.call(a,q.call(a)+e)},M:function(a,e){l.call(a,p.call(a)+e)},s:function(a,e){k.call(a,o.call(a)+e)},l:function(a,e){j.call(a,c.call(a)+e)}};return(dateTime.move=function(a,e,b){a instanceof d||(a=new d(a));r[b](a,e);return a}).apply(null,arguments)},
compare:function(){var d=x.Date,a=d.prototype,g=a.setHours,h=a.getMonth,i=a.getFullYear,j=a.getSeconds,k=a.getMinutes,l=a.getHours,m=a.getTimezoneOffset,f={l:function(a,b){return b-a},s:function(a,b){var c=j;return f.M(a,b)*60+c.call(b)-c.call(a)},M:function(a,b){var c=k;return f.h(a,b)*60+c.call(b)-c.call(a)},h:function(a,b){var c=l;return f.d(a,b)*24+c.call(b)-c.call(a)},d:function(a,b){var c=g,f=m,f=(f.call(b)-f.call(a))*6E4;return~~((c.call(new d(b),0)-c.call(new d(a),0)+f)/864E5)},w:function(a,b){return~~(f.d(a,b)/7)},m:function(a,b){var c=h;return f.y(a,b)*12+c.call(b)-c.call(a)},q:function(a,b){return~~(f.m(a,b)/3)},y:function(a,b){var c=i;return c.call(b)-c.call(a)}};return(dateTime.compare=function(a,b,c){a instanceof d||(a=new d(a));b instanceof d||(b=new d(b));return f[c](a,b)}).apply(null,arguments)},
hoursFromStandard:function(d){var a=x.Date,g=a.prototype;d instanceof a||new a(d);var h=g.getTimezoneOffset,i=g.getFullYear.call(d),g=new a(i,0,1),a=new a(i,6,1);return(x.Math.max(h.call(g),h.call(a))-h.call(d))/60},
isDayLightSavings:function(d){return!!dateTime.hoursFromStandard(d)},
isLeapYear:function(d){return(new x.Date(d,1,29)).getDate()===29}
};
// format
(function(g){var q=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],r=["January","February","March","April","May","June","July","August","September","October","November","December"],v={shortDate:"m/d/yy",mediumDate:"mmm d, yyyy",longDate:"mmmm d, yyyy",fullDate:"dddd, mmmm d, yyyy",shortTime:"h:MMt",mediumTime:"h:MM:ss TT",longTime:"h:MM:ss TT Z",isoDate:"yyyy-mm-dd",isoTime:"HH:MM:ss",isoDateTime:'yyyy-mm-dd"T"HH:MM:ss'},w=function(d){r=d},x=function(d){q=d},y=function(d,g){v[d]=g},o=g.dateTime.format=function(){var d=["th","st","nd","rd"],o=/d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTtz])\1?|[LlrZ]|"([^"]*)"|'([^']*)'/g,B=/[^(]+(?=\))/,C=/\b\w/g,s=g.Date,a=s.prototype,D=a.toTimeString,h,i,e,j,k,l,m,b,E=h=a.getDate,F=i=a.getDay,G=e=a.getMonth,H=j=a.getFullYear,I=k=a.getMilliseconds,J=l=a.getSeconds,K=m=a.getMinutes,L=b=a.getHours,M=a.getUTCDate,N=a.getUTCDay,O=a.getUTCMonth,P=a.getUTCFullYear,Q=a.getUTCMilliseconds,R=a.getUTCSeconds,S=a.getUTCMinutes,T=a.getUTCHours,p=!1,U=function(a){a&&!p?(h=M,i=N,e=O,j=P,k=Q,l=R,m=S,b=T,p=!0):!a&&p&&(h=E,i=F,e=G,j=H,k=I,l=J,m=K,b=L,p=!1)},n=g.String,a=n.prototype,t=a.slice,V=a.replace,W=a.split,z=a.match,X=g.Array.prototype.join,f=function(a,c){a=n(a);for(c!==void 0||(c=2);a.length<c;)a="0"+a;return a},u=function(a){return z.call(D.call(a),B)[0]},A={d:h,dd:function(){return f(h.call(this))},ddd:function(){var a=q[i.call(this)];return t.call(n(a),0,3)},dddd:function(){return q[i.call(this)]},m:function(){return e.call(this)+1},mm:function(){return f(e.call(this)+1)},mmm:function(){var a=r[e.call(this)];return t.call(n(a),0,3)},mmmm:function(){return r[e.call(this)]},yy:function(){return t.call(n(j.call(this)),2)},yyyy:j,h:function(){return b.call(this)%12||12},hh:function(){return f(b.call(this)%12||12)},H:b,HH:function(){return f(b.call(this))},M:m,MM:function(){return f(m.call(this))},s:l,ss:function(){return f(l.call(this))},l:k,L:function(){return f(k.call(this),3)},t:function(){return b.call(this)<12?"a":"p"},tt:function(){return b.call(this)<12?"am":"pm"},T:function(){return b.call(this)<12?"A":"P"},TT:function(){return b.call(this)<12?"AM":"PM"},z:function(){return W.call(u(this)," ")[0]},zz:function(){return u(this)},Z:function(){return X.call(z.call(u(this),C),"")},r:function(){var a=h.call(this),c=a%10;return d[c>3?0:(a-c!==10)*c]}},a=dateTime.format=function(a,c,b){a instanceof s||(a=new s(a));c=v[c]||c;U(b);return V.call(c,o,function(b,c,d,e){return b in A?A[b].call(a):d||e})};a.setMonths=w;a.setWeekdays=x;a.setMask=y;return a.apply(null,arguments)};o.setMonths=w;o.setWeekdays=x;o.setMask=y}(x));
// add format to Date.prototype
(function(b){var a=b.Date.prototype;if(!a.format)a.format=function(a){return b.dateTime.format.call(null,this,a)}}(x));
// add move to Date.prototype
(function(b){var a=b.Date.prototype;if(!a.move)a.move=function(a,c){b.dateTime.move.call(null,this,a,c)}}(x));
}(this));