-
Notifications
You must be signed in to change notification settings - Fork 3
/
xdate.js
18 lines (18 loc) · 6.83 KB
/
xdate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
XDate v0.5
Docs & Licensing: http://arshaw.com/xdate/
*/
var XDate=function(g,m,A,q){function f(){var a=this instanceof f?this:new f,c=arguments,b=c.length,d;typeof c[b-1]=="boolean"&&(d=c[--b],c=p(c,0,b));if(b)if(b==1)if(b=c[0],b instanceof g||typeof b=="number")a[0]=new g(+b);else if(b instanceof f){var c=a,k=new g(+b[0]);if(i(b))k.toString=u;c[0]=k}else{if(typeof b=="string"){a[0]=new g(0);a:{for(var c=b,b=d||!1,k=f.parsers,r=0,e;r<k.length;r++)if(e=k[0](c,b,a)){a=e;break a}a[0]=new g(c)}}}else a[0]=new g(n.apply(g,c)),d||(a[0]=v(a[0]));else a[0]=new g;
typeof d=="boolean"&&B(a,d);return a}function i(a){return a[0].toString===u}function B(a,c,b){if(c){if(!i(a))b&&(a[0]=new g(n(a[0].getFullYear(),a[0].getMonth(),a[0].getDate(),a[0].getHours(),a[0].getMinutes(),a[0].getSeconds(),a[0].getMilliseconds()))),a[0].toString=u}else i(a)&&(a[0]=b?v(a[0]):new g(+a[0]));return a}function C(a,c,b,d,k){var e=j(l,a[0],k),a=j(K,a[0],k),k=c==1?b%12:e(1),f=!1;d.length==2&&typeof d[1]=="boolean"&&(f=d[1],d=[b]);a(c,d);f&&e(1)!=k&&(a(1,[e(1)-1]),a(2,[D(e(0),e(1))]))}
function E(a,c,b,d){var b=Number(b),e=m.floor(b);a["set"+o[c]](a["get"+o[c]]()+e,d||!1);e!=b&&c<6&&E(a,c+1,(b-e)*F[c],d)}function G(a,c,b){var a=a.clone().setUTCMode(!0,!0),c=f(c).setUTCMode(!0,!0),d=0;if(b==0||b==1){for(var e=6;e>=b;e--)d/=F[e],d+=l(c,!1,e)-l(a,!1,e);b==1&&(d+=(c.getFullYear()-a.getFullYear())*12)}else b==2?(b=a.toDate().setUTCHours(0,0,0,0),d=c.toDate().setUTCHours(0,0,0,0),d=m.round((d-b)/864E5)+(c-d-(a-b))/864E5):d=(c-a)/[36E5,6E4,1E3,1][b-3];return d}function s(a){var c=a(0),
b=a(1),a=a(2),b=new g(n(c,b,a)),d=w(c),a=d;b<d?a=w(c-1):(c=w(c+1),b>=c&&(a=c));return m.floor(m.round((b-a)/864E5)/7)+1}function w(a){a=new g(n(a,0,4));a.setUTCDate(a.getUTCDate()-(a.getUTCDay()+6)%7);return a}function H(a,c,b,d,e){var r=f.locales,g=r[f.defaultLocale]||{},h=j(l,a,e),b=(typeof b=="string"?r[b]:b)||{};return x(a,c,function(a){if(d)for(var b=(a==7?2:a)-1;b>=0;b--)d.push(h(b));return h(a)},function(a){return b[a]||g[a]},e)}function x(a,c,b,d,e){for(var f,g,h="";f=c.match(L);){h+=c.substr(0,
f.index);if(f[1]){g=h;for(var h=a,i=f[1],l=b,m=d,n=e,j=i.length,o=void 0,p="";j>0;)o=M(h,i.substr(0,j),l,m,n),o!==q?(p+=o,i=i.substr(j),j=i.length):j--;h=g+(p+i)}else f[3]?(g=x(a,f[4],b,d,e),parseInt(g.replace(/\D/g,""),10)&&(h+=g)):h+=f[7]||"'";c=c.substr(f.index+f[0].length)}return h+c}function M(a,c,b,d,e){var g=f.formatters[c];if(typeof g=="string")return x(a,g,b,d,e);else if(typeof g=="function")return g(a,e||!1,d);switch(c){case "fff":return h(b(6),3);case "s":return b(5);case "ss":return h(b(5));
case "m":return b(4);case "mm":return h(b(4));case "h":return b(3)%12||12;case "hh":return h(b(3)%12||12);case "H":return b(3);case "HH":return h(b(3));case "d":return b(2);case "dd":return h(b(2));case "ddd":return d("dayNamesShort")[b(7)]||"";case "dddd":return d("dayNames")[b(7)]||"";case "M":return b(1)+1;case "MM":return h(b(1)+1);case "MMM":return d("monthNamesShort")[b(1)]||"";case "MMMM":return d("monthNames")[b(1)]||"";case "yy":return(b(0)+"").substring(2);case "yyyy":return b(0);case "t":return t(b,
d).substr(0,1).toLowerCase();case "tt":return t(b,d).toLowerCase();case "T":return t(b,d).substr(0,1);case "TT":return t(b,d);case "z":case "zz":case "zzz":return e?c="Z":(d=a.getTimezoneOffset(),a=d<0?"+":"-",b=m.floor(m.abs(d)/60),d=m.abs(d)%60,e=b,c=="zz"?e=h(b):c=="zzz"&&(e=h(b)+":"+h(d)),c=a+e),c;case "w":return s(b);case "ww":return h(s(b));case "S":return c=b(2),c>10&&c<20?"th":["st","nd","rd"][c%10-1]||"th"}}function t(a,c){return a(3)<12?c("amDesignator"):c("pmDesignator")}function y(a){return!isNaN(+a[0])}
function l(a,c,b){return a["get"+(c?"UTC":"")+o[b]]()}function K(a,c,b,d){a["set"+(c?"UTC":"")+o[b]].apply(a,d)}function v(a){return new g(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate(),a.getUTCHours(),a.getUTCMinutes(),a.getUTCSeconds(),a.getUTCMilliseconds())}function D(a,c){return 32-(new g(n(a,c,32))).getUTCDate()}function z(a){return function(){return a.apply(q,[this].concat(p(arguments)))}}function j(a){var c=p(arguments,1);return function(){return a.apply(q,c.concat(p(arguments)))}}function p(a,
c,b){return A.prototype.slice.call(a,c||0,b===q?a.length:b)}function I(a,c){for(var b=0;b<a.length;b++)c(a[b],b)}function h(a,c){c=c||2;for(a+="";a.length<c;)a="0"+a;return a}var o="FullYear,Month,Date,Hours,Minutes,Seconds,Milliseconds,Day,Year".split(","),J=["Years","Months","Days"],F=[12,31,24,60,60,1E3,1],L=/(([a-zA-Z])\2*)|(\((('.*?'|\(.*?\)|.)*?)\))|('(.*?)')/,n=g.UTC,u=g.prototype.toUTCString,e=f.prototype;e.length=1;e.splice=A.prototype.splice;e.getUTCMode=z(i);e.setUTCMode=z(B);e.getTimezoneOffset=
function(){return i(this)?0:this[0].getTimezoneOffset()};I(o,function(a,c){e["get"+a]=function(){return l(this[0],i(this),c)};c!=8&&(e["getUTC"+a]=function(){return l(this[0],!0,c)});c!=7&&(e["set"+a]=function(a){C(this,c,a,arguments,i(this));return this},c!=8&&(e["setUTC"+a]=function(a){C(this,c,a,arguments,!0);return this},e["add"+(J[c]||a)]=function(a,d){E(this,c,a,d);return this},e["diff"+(J[c]||a)]=function(a){return G(this,a,c)}))});e.getWeek=function(){return s(j(l,this,!1))};e.getUTCWeek=
function(){return s(j(l,this,!0))};e.addWeeks=function(a){return this.addDays(Number(a)*7)};e.diffWeeks=function(a){return G(this,a,2)/7};f.parsers=[function(a,c,b){if(a=a.match(/^(\d{4})(-(\d{2})(-(\d{2})([T ](\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|(([-+])(\d{2})(:?(\d{2}))?))?)?)?)?$/)){var d=new g(n(a[1],a[3]?a[3]-1:0,a[5]||1,a[7]||0,a[8]||0,a[10]||0,a[12]?Number("0."+a[12])*1E3:0));a[13]?a[14]&&d.setUTCMinutes(d.getUTCMinutes()+(a[15]=="-"?1:-1)*(Number(a[16])*60+(a[18]?Number(a[18]):0))):c||(d=
v(d));return b.setTime(+d)}}];f.parse=function(a){return+f(""+a)};e.toString=function(a,c,b){return a===q||!y(this)?this[0].toString():H(this,a,c,b,i(this))};e.toUTCString=e.toGMTString=function(a,c,b){return a===q||!y(this)?this[0].toUTCString():H(this,a,c,b,!0)};e.toISOString=function(){return this.toUTCString("yyyy-MM-dd'T'HH:mm:ss(.fff)zzz")};f.defaultLocale="";f.locales={"":{monthNames:"January,February,March,April,May,June,July,August,September,October,November,December".split(","),monthNamesShort:"Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec".split(","),
dayNames:"Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(","),dayNamesShort:"Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(","),amDesignator:"AM",pmDesignator:"PM"}};f.formatters={i:"yyyy-MM-dd'T'HH:mm:ss(.fff)",u:"yyyy-MM-dd'T'HH:mm:ss(.fff)zzz"};I("getTime,valueOf,toDateString,toTimeString,toLocaleString,toLocaleDateString,toLocaleTimeString,toJSON".split(","),function(a){e[a]=function(){return this[0][a]()}});e.setTime=function(a){this[0].setTime(a);return this};e.valid=z(y);e.clone=function(){return new f(this)};
e.clearTime=function(){return this.setHours(0,0,0,0)};e.toDate=function(){return new g(+this[0])};f.now=function(){return+new g};f.today=function(){return(new f).clearTime()};f.UTC=n;f.getDaysInMonth=D;return f}(Date,Math,Array);