datex.js 1.0.10 → 1.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ module.exports = {
2
+ presets: ['@babel/preset-env'],
3
+ plugins: [
4
+ '@babel/plugin-transform-runtime',
5
+ '@babel/plugin-transform-class-properties',
6
+ [
7
+ '@babel/plugin-syntax-import-attributes',
8
+ {
9
+ deprecatedAssertSyntax: true
10
+ }
11
+ ]
12
+ ]
13
+ }
package/dist/datex.min.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * datex.js v1.0.10
2
+ * datex.js v1.0.11
3
3
  * a datetime format library
4
4
  * https://passer-by.com/datex/
5
5
  *
@@ -8,6 +8,6 @@
8
8
  * Released under the MIT License
9
9
  * https://github.com/mumuy/datex
10
10
  *
11
- * Created on: 2023-06-29
11
+ * Created on: 2023-10-27
12
12
  */
13
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).datex=e()}(this,(function(){"use strict";let t={"en-us":{MMM:["Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.","Dec."],MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],Do:["1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"],WW:["Sun.","Mon.","Tues.","Wed.","Thur.","Fri.","Sat."],WWW:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]},"zh-cn":{MMM:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],MMMM:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],Do:["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日","27日","28日","29日","30日","31日"],WW:["周日","周一","周二","周三","周四","周五","周六"],WWW:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]}},e="en-us";"undefined"!=typeof self&&self.navigator&&(e=self.navigator.language.toLowerCase(),t[e]||(e="en-us"));let n=Intl.DateTimeFormat().resolvedOptions().timeZone,i=0;const s=["year","month","day","hour","minute","second","millsecond"],a=[1970,1,1,0,0,0,0],r=(t,e)=>new Date(t.toLocaleString("en-US",{timeZone:e}));function o(...t){return new o.prototype.init(...t)}function h(t){return t instanceof o?t:o(t)}return o.setLanguage=function(e,n={}){return t[e]=Object.assign(t[e]||{},n),this},o.switchLanguage=function(t){return e=t,this},o.now=Date.now,o.supportedTimezones="undefined"!=typeof Intl&&Intl.supportedValuesOf?Intl.supportedValuesOf("timeZone"):[],o.switchTimezone=function(t){n=t,i=r(new Date("1970/1/1"),n).getTime()-new Date("1970/1/1").getTime()},o.getTimezone=function(){return n},o.getTimezoneOffset=function(){return(new Date).getTimezoneOffset()-i/6e4},o.prototype={_date:null,_langMap:{},_lang:null,_timezone:null,_offset:0,init:function(...t){if(t.length){if(t[0]instanceof Date)this._date=t[0];else{if(Array.isArray(t[0])?t=a.map(((e,n)=>t[0][n]||e)):null==(e=t[0])||"object"!=typeof e&&"function"!=typeof e||(t=a.map(((e,n)=>t[0][s[n]]||e))),1==t.length&&"string"==typeof t[0]){let e=t[0].match(/(\d{1,4})[\-\/](\d{1,2})[\-\/](\d{1,2})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),n=t[0].match(/(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{3,4})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),i=t[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);e&&!n?t=[1,2,3,5,6,8,10].map((function(t,n){return+(e[t]||a[n])})):n?t=[3,1,2,5,6,8,10].map((function(t,e){return+(n[t]||a[e])})):i&&(t=[1,2,3,4,5,6,7].map((function(t,e){return+(i[t]||a[e])})))}t.length>=3&&t[1]--,this._date=new Date(...t),t.length>=2&&!isNaN(t[0])&&t[0]<100&&this._date.setFullYear(t[0])}i&&this._date.setTime(this._date.getTime()-i)}else this._date=new Date;var e;return this},getTime(){return this._date.getTime()},getUnix(){return~~(this._date.getTime()/1e3)},clone(){return o(this.getTime())},toDate(){return this._date},toObject(){let t=this._date;return{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),second:t.getSeconds(),millsecond:t.getMilliseconds(),timestamp:t.getTime(),week:t.getDay()}},toArray(){let t=this.toObject();return s.map((e=>t[e]))},toString(){return this._date.toString()},toISOString(){return this._date.toISOString()},set(t,e){let n=this._date,i=this.toObject();switch(t){case"year":n.setFullYear(e);break;case"month":n.setMonth(e-1);break;case"day":n.setDate(e);break;case"hour":n.setHours(e);break;case"minute":n.setMinutes(e);break;case"second":n.setSeconds(e);break;case"millsecond":n.setMilliseconds(e);break;case"timestamp":n.setTime(e);break;case"week":n.setDate(i.day-i.week+e)}return this},get(t){return this.toObject()[t]},change(t,e){let n=this.toObject();return this.set(t,n[t]+e)},format(n="YYYY-MM-DD HH:mm:ss"){let s=this.clone(),a=this._offset||i;s._date.setTime(this._date.getTime()+a);let r=s._date,o=s.toObject(),h=r.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),d={YYYY:""+o.year,YY:(""+o.year).padStart(2,"0"),MM:(""+o.month).padStart(2,"0"),M:""+o.month,DD:(""+o.day).padStart(2,"0"),D:""+o.day,HH:(""+o.hour).padStart(2,"0"),H:""+o.hour,hh:(""+o.hour%12).padStart(2,"0"),h:""+o.hour%12,mm:(""+o.minute).padStart(2,"0"),m:""+o.minute,ss:(""+o.second).padStart(2,"0"),s:""+o.second,S:""+~~(o.millsecond%1e3/100),SS:""+~~(o.millsecond%1e3/10),SSS:""+o.millsecond%1e3,Z:h[1]+h[2]+":"+h[3],ZZ:h[1]+h[2]+h[3],A:["AM","PM"][~~(o.hour/12)],a:["am","pm"][~~(o.hour/12)],X:o.timestamp,x:~~(o.timestamp/1e3),Q:""+~~(o.month/3),W:o.week},u=Object.assign({},t,this._langMap)[this._lang||e];return d.MMM=u.MMM[o.month-1],d.MMMM=u.MMMM[o.month-1],d.Do=u.Do[o.day-1],d.WW=u.WW[o.week],d.WWW=u.WWW[o.week],n.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,(function(t){return d[t]||""}))},startOf(t){let e=this.toObject(),n=null,i=s.indexOf(t)+1,r=this.toArray(),h=a.slice(i);return r.splice(i,h.length,...h),n="timestamp"==t?this.clone():"week"==t?o(e.year,e.month,e.day-e.week,0,0,0,0):o(...r),n},endOf(t){return this.startOf(t).change(t,"week"==t?7:1).change("millsecond",-1)},diffWith(t,e){if(!(t=h(t)).isValid())return!1;let n={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1},i=this.getTime()-t.getTime(),a=0;if(e){if(n[e])a=~~(i/n[e]);else if("month"==e){a=12*(this.get("year")-1)+this.get("month")-(12*(t.get("year")-1)+t.get("month")),a<0&&this.get("day")>t.get("day")?a+=1:a>0&&this.get("day")<t.get("day")&&(a-=1)}else"year"==e&&(a=this.get("year")-t.get("year"),a<0&&(this.get("month")>t.get("month")||this.get("month")==t.get("month")&&this.get("day")>t.get("day"))?a+=1:a>0&&(this.get("month")<t.get("month")||this.get("month")==t.get("month")&&this.get("day")<t.get("day"))&&(a-=1));return a}{let e=this.clone(),n={};return s.forEach((function(i){n[i]=e.diffWith(t,i),e.set(i,t.get(i))})),n}},isBefore(t,e="timestamp"){return t=h(t),this.get(e)<t.get(e)},isAfter(t,e="timestamp"){return t=h(t),this.get(e)>t.get(e)},isSame(t,e="timestamp"){return t=h(t),this.get(e)==t.get(e)},isBetween(t,e,n="timestamp"){return t=h(t),e=h(e),this.get(n)>t.get(n)&&this.get(n)<e.get(n)},setLanguage(t,e={}){return t=t.toLowerCase(),this._langMap[t]=Object.assign(this._langMap[t]||{},e),this},switchLanguage(t){return this._lang=t.toLowerCase(),this},switchTimezone(t){return this._timezone=t,this._offset=r(new Date("1970/1/1"),this._timezone).getTime()-new Date("1970/1/1").getTime(),this},getTimezone(){return this._timezone||n},getTimezoneOffset(){return this._date.getTimezoneOffset()-(this._offset||i)/6e4},isValid(){return!isNaN(this.getTime())}},o.prototype.init.prototype=o.prototype,o}));
13
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).datex=e()}(this,(function(){"use strict";function t(e,n){return t=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},t(e,n)}function e(n,i,r){return e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}()?Reflect.construct.bind():function(e,n,i){var r=[null];r.push.apply(r,n);var o=new(Function.bind.apply(e,r));return i&&t(o,i.prototype),o},e.apply(null,arguments)}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=new Array(e);n<e;n++)i[n]=t[n];return i}function i(t){return function(t){if(Array.isArray(t))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return n(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?n(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)}var o=["year","month","day","hour","minute","second","millsecond"],a=[1970,1,1,0,0,0,0],s={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1};var u=[],c={_date:null,init:function(){for(var t=arguments.length,n=new Array(t),s=0;s<t;s++)n[s]=arguments[s];if(n.length){if(Object.getPrototypeOf(n[0])==Object.getPrototypeOf(this))return n[0];if(n[0]instanceof Date)this._date=n[0];else{if(Array.isArray(n[0])?n=a.map((function(t,e){return n[0][e]||t})):null==(d=n[0])||"object"!=r(d)&&"function"!=typeof d||(n=a.map((function(t,e){return n[0][o[e]]||t}))),1==n.length&&"string"==typeof n[0]){var c=n[0].match(/(\d{1,4})[\-\/](\d{1,2})[\-\/](\d{1,2})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),h=n[0].match(/(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{3,4})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),f=n[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);c&&!h?n=[1,2,3,5,6,8,10].map((function(t,e){return+(c[t]||a[e])})):h?n=[3,1,2,5,6,8,10].map((function(t,e){return+(h[t]||a[e])})):f&&(n=[1,2,3,4,5,6,7].map((function(t,e){return+(f[t]||a[e])})))}n.length>=3&&n[1]--,this._date=e(Date,i(n)),n.length>=2&&!isNaN(n[0])&&n[0]<100&&this._date.setFullYear(n[0])}}else this._date=new Date;var d,g=this;return u.forEach((function(t){t.bind(g).apply(void 0,i(n))})),this},onInit:function(t){u.push(t)}};function h(){for(var t=arguments.length,n=new Array(t),i=0;i<t;i++)n[i]=arguments[i];return e(h.prototype.init,n)}h.now=Date.now,h.extend=function(t){t(h,c)},h.prototype=c,c.init.prototype=c;var f={MMM:["Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.","Dec."],MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],Do:["1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"],WW:["Sun.","Mon.","Tues.","Wed.","Thur.","Fri.","Sat."],WWW:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]},d={MMM:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],MMMM:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],Do:["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日","27日","28日","29日","30日","31日"],WW:["周日","周一","周二","周三","周四","周五","周六"],WWW:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]};return[function(t,e){Object.assign(e,{toDate:function(){return this._date},toObject:function(){var t=this._date;return{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),second:t.getSeconds(),millsecond:t.getMilliseconds(),timestamp:t.getTime(),week:t.getDay()}},toArray:function(){var t=this.toObject();return o.map((function(e){return t[e]}))},toString:function(){return this._date.toString()},toISOString:function(){return this._date.toISOString()},getTime:function(){return this._date.getTime()},getUnix:function(){return~~(this._date.getTime()/1e3)},clone:function(){var e=this,n=t(this.getTime());return Object.getOwnPropertyNames(e).forEach((function(t){"_date"!=t&&(n[t]=e[t])})),n},isValid:function(){return!isNaN(this.getTime())},set:function(t,e){var n=this._date,i=this.toObject();switch(t){case"year":n.setFullYear(e);break;case"month":n.setMonth(e-1);break;case"day":n.setDate(e);break;case"hour":n.setHours(e);break;case"minute":n.setMinutes(e);break;case"second":n.setSeconds(e);break;case"millsecond":n.setMilliseconds(e);break;case"timestamp":n.setTime(e);break;case"week":n.setDate(i.day-i.week+e)}return this},change:function(t,e){var n=this.toObject();return void 0!==s[t]?this.set("timestamp",n.timestamp+e*s[t]):this.set(t,n[t]+e)},get:function(t){return this.toObject()[t]},format:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD HH:mm:ss",e=this.clone(),n=e._date,i=e.toObject(),r=n.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),o={YYYY:""+i.year,YY:(""+i.year).padStart(2,"0"),MM:(""+i.month).padStart(2,"0"),M:""+i.month,DD:(""+i.day).padStart(2,"0"),D:""+i.day,HH:(""+i.hour).padStart(2,"0"),H:""+i.hour,hh:(""+i.hour%12).padStart(2,"0"),h:""+i.hour%12,mm:(""+i.minute).padStart(2,"0"),m:""+i.minute,ss:(""+i.second).padStart(2,"0"),s:""+i.second,S:""+~~(i.millsecond%1e3/100),SS:""+~~(i.millsecond%1e3/10),SSS:""+i.millsecond%1e3,Z:r[1]+r[2]+":"+r[3],ZZ:r[1]+r[2]+r[3],A:["AM","PM"][~~(i.hour/12)],a:["am","pm"][~~(i.hour/12)],X:i.timestamp,x:~~(i.timestamp/1e3),Q:""+~~(i.month/3),W:i.week};return t.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|A|a|X|x|Q|W+/g,(function(t){return o[t]||""}))}})},function(t,e){Object.assign(e,{diffWith:function(e,n){if(!(e=t(e)).isValid())return!1;var i=this.getTime()-e.getTime(),r=0;if(n){if(s[n])r=~~(i/s[n]);else if("month"==n){(r=12*(this.get("year")-1)+this.get("month")-(12*(e.get("year")-1)+e.get("month")))<0&&this.get("day")>e.get("day")?r+=1:r>0&&this.get("day")<e.get("day")&&(r-=1)}else"year"==n&&((r=this.get("year")-e.get("year"))<0&&(this.get("month")>e.get("month")||this.get("month")==e.get("month")&&this.get("day")>e.get("day"))?r+=1:r>0&&(this.get("month")<e.get("month")||this.get("month")==e.get("month")&&this.get("day")<e.get("day"))&&(r-=1));return r}var a=this.clone(),u={};return o.forEach((function(t){u[t]=a.diffWith(e,t),a.set(t,e.get(t))})),u},endOf:function(t){return this.startOf(t).change(t,"week"==t?7:1).change("millsecond",-1)},startOf:function(e){var n=this.toObject(),r=o.indexOf(e)+1,s=this.toArray(),u=a.slice(r);return s.splice.apply(s,[r,u.length].concat(i(u))),"timestamp"==e?this.clone():"week"==e?t(n.year,n.month,n.day-n.week,0,0,0,0):t.apply(void 0,i(s))}})},function(t,e){Object.assign(e,{isAfter:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"timestamp";return e=t(e),this.get(n)>e.get(n)},isBefore:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"timestamp";return e=t(e),this.get(n)<e.get(n)},isBetween:function(e,n){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"timestamp";return e=t(e),n=t(n),this.get(i)>e.get(i)&&this.get(i)<n.get(i)},isSame:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"timestamp";return e=t(e),this.get(n)==e.get(n)}})},function(t,e){var n={};n["en-us"]=f,n["zh-cn"]=d;var i="en-us";"undefined"!=typeof self&&self.navigator&&(i=self.navigator.language.toLowerCase(),n[i]||(i="en-us")),Object.assign(t,{setLanguage:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return t=t.toLowerCase(),n[t]=Object.assign(n[t]||{},e),this},switchLanguage:function(t){return i=t.toLowerCase(),this},getLanguage:function(){return n[i]}}),Object.assign(e,{_langMap:{},_lang:null,setLanguage:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return t=t.toLowerCase(),this._langMap[t]=Object.assign(this._langMap[t]||{},e),this},switchLanguage:function(t){return this._lang=t.toLowerCase(),this},getLanguage:function(){return this._langMap[this._lang]}}),e.onInit((function(){this._langMap=Object.assign(this._langMap,n),this._lang=i}));var r=e.format;Object.assign(e,{format:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD HH:mm:ss",n=this.clone().toObject(),i=t.getLanguage()||this.getLanguage(),o={};for(var a in o.MMM=i.MMM[n.month-1],o.MMMM=i.MMMM[n.month-1],o.Do=i.Do[n.day-1],o.WW=i.WW[n.week],o.WWW=i.WWW[n.week],o)e=e.replace(a,o[a]||"");return r.bind(this)(e)}})},function(t,e){var n=Intl.DateTimeFormat().resolvedOptions().timeZone,i=0,r=function(t,e){return new Date(t.toLocaleString("en-US",{timeZone:e}))};Object.assign(t,{supportedTimezones:"undefined"!=typeof Intl&&Intl.supportedValuesOf?Intl.supportedValuesOf("timeZone"):[],switchTimezone:function(t){return n=t,i=r(new Date("1970/1/1"),n).getTime()-new Date("1970/1/1").getTime(),this},getTimezoneOffset:function(){return(new Date).getTimezoneOffset()-i/6e4},getTimezone:function(){return n}}),Object.assign(e,{_timezone:null,_offset:0,switchTimezone:function(t){return this._timezone=t,this._offset=r(new Date("1970/1/1"),this._timezone).getTime()-new Date("1970/1/1").getTime(),this},getTimezoneOffset:function(){return this._date.getTimezoneOffset()-this._offset/6e4},getTimezone:function(){return this._timezone}}),e.onInit((function(){this._timezone=n,this._offset=i,arguments.length&&((arguments.length<=0?void 0:arguments[0])instanceof Date||(arguments.length<=0?void 0:arguments[0])instanceof t||1==arguments.length&&"number"==typeof(arguments.length<=0?void 0:arguments[0])||i&&this._date.setTime(this._date.getTime()-i))}));var o=e.format;Object.assign(e,{format:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD HH:mm:ss",e=this.clone();return e._date.setTime(e._date.getTime()+e._offset),o.bind(e)(t)}})}].forEach(h.extend),h}));
@@ -0,0 +1,13 @@
1
+ /*!
2
+ * datex.js v1.0.11
3
+ * a datetime format library
4
+ * https://passer-by.com/datex/
5
+ *
6
+ * Copyright (c) 2022-present, HaoLe Zheng
7
+ *
8
+ * Released under the MIT License
9
+ * https://github.com/mumuy/datex
10
+ *
11
+ * Created on: 2023-10-27
12
+ */
13
+ function t(e,n){return t=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},t(e,n)}function e(n,r,i){return e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}()?Reflect.construct.bind():function(e,n,r){var i=[null];i.push.apply(i,n);var o=new(Function.bind.apply(e,i));return r&&t(o,r.prototype),o},e.apply(null,arguments)}function n(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function r(t){return function(t){if(Array.isArray(t))return n(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return n(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i(t)}var o=["year","month","day","hour","minute","second","millsecond"],a=[1970,1,1,0,0,0,0],s={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1};var u=[],c={_date:null,init:function(){for(var t=arguments.length,n=new Array(t),s=0;s<t;s++)n[s]=arguments[s];if(n.length){if(Object.getPrototypeOf(n[0])==Object.getPrototypeOf(this))return n[0];if(n[0]instanceof Date)this._date=n[0];else{if(Array.isArray(n[0])?n=a.map((function(t,e){return n[0][e]||t})):null==(g=n[0])||"object"!=i(g)&&"function"!=typeof g||(n=a.map((function(t,e){return n[0][o[e]]||t}))),1==n.length&&"string"==typeof n[0]){var c=n[0].match(/(\d{1,4})[\-\/](\d{1,2})[\-\/](\d{1,2})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),h=n[0].match(/(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{3,4})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),f=n[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);c&&!h?n=[1,2,3,5,6,8,10].map((function(t,e){return+(c[t]||a[e])})):h?n=[3,1,2,5,6,8,10].map((function(t,e){return+(h[t]||a[e])})):f&&(n=[1,2,3,4,5,6,7].map((function(t,e){return+(f[t]||a[e])})))}n.length>=3&&n[1]--,this._date=e(Date,r(n)),n.length>=2&&!isNaN(n[0])&&n[0]<100&&this._date.setFullYear(n[0])}}else this._date=new Date;var g,d=this;return u.forEach((function(t){t.bind(d).apply(void 0,r(n))})),this},onInit:function(t){u.push(t)}};function h(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];return e(h.prototype.init,n)}h.now=Date.now,h.extend=function(t){t(h,c)},h.prototype=c,c.init.prototype=c;var f={MMM:["Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.","Dec."],MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],Do:["1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"],WW:["Sun.","Mon.","Tues.","Wed.","Thur.","Fri.","Sat."],WWW:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]},g={MMM:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],MMMM:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],Do:["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日","27日","28日","29日","30日","31日"],WW:["周日","周一","周二","周三","周四","周五","周六"],WWW:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]};[function(t,e){Object.assign(e,{toDate:function(){return this._date},toObject:function(){var t=this._date;return{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),second:t.getSeconds(),millsecond:t.getMilliseconds(),timestamp:t.getTime(),week:t.getDay()}},toArray:function(){var t=this.toObject();return o.map((function(e){return t[e]}))},toString:function(){return this._date.toString()},toISOString:function(){return this._date.toISOString()},getTime:function(){return this._date.getTime()},getUnix:function(){return~~(this._date.getTime()/1e3)},clone:function(){var e=this,n=t(this.getTime());return Object.getOwnPropertyNames(e).forEach((function(t){"_date"!=t&&(n[t]=e[t])})),n},isValid:function(){return!isNaN(this.getTime())},set:function(t,e){var n=this._date,r=this.toObject();switch(t){case"year":n.setFullYear(e);break;case"month":n.setMonth(e-1);break;case"day":n.setDate(e);break;case"hour":n.setHours(e);break;case"minute":n.setMinutes(e);break;case"second":n.setSeconds(e);break;case"millsecond":n.setMilliseconds(e);break;case"timestamp":n.setTime(e);break;case"week":n.setDate(r.day-r.week+e)}return this},change:function(t,e){var n=this.toObject();return void 0!==s[t]?this.set("timestamp",n.timestamp+e*s[t]):this.set(t,n[t]+e)},get:function(t){return this.toObject()[t]},format:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD HH:mm:ss",e=this.clone(),n=e._date,r=e.toObject(),i=n.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),o={YYYY:""+r.year,YY:(""+r.year).padStart(2,"0"),MM:(""+r.month).padStart(2,"0"),M:""+r.month,DD:(""+r.day).padStart(2,"0"),D:""+r.day,HH:(""+r.hour).padStart(2,"0"),H:""+r.hour,hh:(""+r.hour%12).padStart(2,"0"),h:""+r.hour%12,mm:(""+r.minute).padStart(2,"0"),m:""+r.minute,ss:(""+r.second).padStart(2,"0"),s:""+r.second,S:""+~~(r.millsecond%1e3/100),SS:""+~~(r.millsecond%1e3/10),SSS:""+r.millsecond%1e3,Z:i[1]+i[2]+":"+i[3],ZZ:i[1]+i[2]+i[3],A:["AM","PM"][~~(r.hour/12)],a:["am","pm"][~~(r.hour/12)],X:r.timestamp,x:~~(r.timestamp/1e3),Q:""+~~(r.month/3),W:r.week};return t.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|A|a|X|x|Q|W+/g,(function(t){return o[t]||""}))}})},function(t,e){Object.assign(e,{diffWith:function(e,n){if(!(e=t(e)).isValid())return!1;var r=this.getTime()-e.getTime(),i=0;if(n){if(s[n])i=~~(r/s[n]);else if("month"==n){(i=12*(this.get("year")-1)+this.get("month")-(12*(e.get("year")-1)+e.get("month")))<0&&this.get("day")>e.get("day")?i+=1:i>0&&this.get("day")<e.get("day")&&(i-=1)}else"year"==n&&((i=this.get("year")-e.get("year"))<0&&(this.get("month")>e.get("month")||this.get("month")==e.get("month")&&this.get("day")>e.get("day"))?i+=1:i>0&&(this.get("month")<e.get("month")||this.get("month")==e.get("month")&&this.get("day")<e.get("day"))&&(i-=1));return i}var a=this.clone(),u={};return o.forEach((function(t){u[t]=a.diffWith(e,t),a.set(t,e.get(t))})),u},endOf:function(t){return this.startOf(t).change(t,"week"==t?7:1).change("millsecond",-1)},startOf:function(e){var n=this.toObject(),i=o.indexOf(e)+1,s=this.toArray(),u=a.slice(i);return s.splice.apply(s,[i,u.length].concat(r(u))),"timestamp"==e?this.clone():"week"==e?t(n.year,n.month,n.day-n.week,0,0,0,0):t.apply(void 0,r(s))}})},function(t,e){Object.assign(e,{isAfter:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"timestamp";return e=t(e),this.get(n)>e.get(n)},isBefore:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"timestamp";return e=t(e),this.get(n)<e.get(n)},isBetween:function(e,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"timestamp";return e=t(e),n=t(n),this.get(r)>e.get(r)&&this.get(r)<n.get(r)},isSame:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"timestamp";return e=t(e),this.get(n)==e.get(n)}})},function(t,e){var n={};n["en-us"]=f,n["zh-cn"]=g;var r="en-us";"undefined"!=typeof self&&self.navigator&&(r=self.navigator.language.toLowerCase(),n[r]||(r="en-us")),Object.assign(t,{setLanguage:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return t=t.toLowerCase(),n[t]=Object.assign(n[t]||{},e),this},switchLanguage:function(t){return r=t.toLowerCase(),this},getLanguage:function(){return n[r]}}),Object.assign(e,{_langMap:{},_lang:null,setLanguage:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return t=t.toLowerCase(),this._langMap[t]=Object.assign(this._langMap[t]||{},e),this},switchLanguage:function(t){return this._lang=t.toLowerCase(),this},getLanguage:function(){return this._langMap[this._lang]}}),e.onInit((function(){this._langMap=Object.assign(this._langMap,n),this._lang=r}));var i=e.format;Object.assign(e,{format:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD HH:mm:ss",n=this.clone().toObject(),r=t.getLanguage()||this.getLanguage(),o={};for(var a in o.MMM=r.MMM[n.month-1],o.MMMM=r.MMMM[n.month-1],o.Do=r.Do[n.day-1],o.WW=r.WW[n.week],o.WWW=r.WWW[n.week],o)e=e.replace(a,o[a]||"");return i.bind(this)(e)}})},function(t,e){var n=Intl.DateTimeFormat().resolvedOptions().timeZone,r=0,i=function(t,e){return new Date(t.toLocaleString("en-US",{timeZone:e}))};Object.assign(t,{supportedTimezones:"undefined"!=typeof Intl&&Intl.supportedValuesOf?Intl.supportedValuesOf("timeZone"):[],switchTimezone:function(t){return n=t,r=i(new Date("1970/1/1"),n).getTime()-new Date("1970/1/1").getTime(),this},getTimezoneOffset:function(){return(new Date).getTimezoneOffset()-r/6e4},getTimezone:function(){return n}}),Object.assign(e,{_timezone:null,_offset:0,switchTimezone:function(t){return this._timezone=t,this._offset=i(new Date("1970/1/1"),this._timezone).getTime()-new Date("1970/1/1").getTime(),this},getTimezoneOffset:function(){return this._date.getTimezoneOffset()-this._offset/6e4},getTimezone:function(){return this._timezone}}),e.onInit((function(){this._timezone=n,this._offset=r,arguments.length&&((arguments.length<=0?void 0:arguments[0])instanceof Date||(arguments.length<=0?void 0:arguments[0])instanceof t||1==arguments.length&&"number"==typeof(arguments.length<=0?void 0:arguments[0])||r&&this._date.setTime(this._date.getTime()-r))}));var o=e.format;Object.assign(e,{format:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DD HH:mm:ss",e=this.clone();return e._date.setTime(e._date.getTime()+e._offset),o.bind(e)(t)}})}].forEach(h.extend);export{h as default};
package/index.html CHANGED
@@ -198,6 +198,12 @@
198
198
  <span class="subtitle">now()</span>
199
199
  </a>
200
200
  </li>
201
+ <li>
202
+ <a href="#hash28">
203
+ <span class="title">插件扩展</span>
204
+ <span class="subtitle">extend()</span>
205
+ </a>
206
+ </li>
201
207
  </ul>
202
208
  </div>
203
209
  </div>
@@ -217,7 +223,11 @@
217
223
  <div class="hd">
218
224
  <h1>datex时间格式化工具<span class="tag">开源算法</span></h1>
219
225
  <p>极度轻巧的时间格式化处理工具,适用于多数网页时间应用场景</p>
220
- <p><a class="btn" href="https://passer-by.com/datex/dist/datex.min.js" target="_blank">获取脚本</a></p>
226
+ <p>
227
+ <a class="btn" href="https://passer-by.com/datex/dist/datex.min.js" target="_blank">获取脚本</a>
228
+ <a class="btn btn-green" href="https://passer-by.com/datex/timezone.html" target="_blank">世界时间</a>
229
+ <a class="btn btn-orange" href="https://passer-by.com/calendar/" target="_blank">中华万年历</a>
230
+ </p>
221
231
  </div>
222
232
  <div class="bd">
223
233
  <div id="hash0" class="box">
@@ -1167,6 +1177,16 @@
1167
1177
  <p>运行:datex.now()</p>
1168
1178
  </div>
1169
1179
  </div>
1180
+ <div id="hash28" class="box">
1181
+ <div class="c-hd">
1182
+ <p class="title">插件扩展</p>
1183
+ <p class="subtitle">extend()</p>
1184
+ </div>
1185
+ <div class="c-bd">
1186
+ <p>全局设置</p>
1187
+ <p>运行:datex.extend(pluginsLoader)</p>
1188
+ </div>
1189
+ </div>
1170
1190
  </div>
1171
1191
  </div>
1172
1192
  </div>
@@ -1184,6 +1204,7 @@
1184
1204
  </div>
1185
1205
  </div>
1186
1206
  </div>
1207
+ <script type="text/javascript" src="https://passer-by.com/public/script/projects.js"></script>
1187
1208
  <div class="mod-foot">
1188
1209
  <div class="inner">
1189
1210
  <div class="bd">
@@ -1191,7 +1212,6 @@
1191
1212
  </div>
1192
1213
  </div>
1193
1214
  </div>
1194
- <script type="text/javascript" src="https://passer-by.com/public/script/projects.js"></script>
1195
1215
  </div>
1196
1216
  </div>
1197
1217
  <script type="text/javascript" src="https://jquerywidget.com/public/script/jquery.min.js"></script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datex.js",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "a datetime format library",
5
5
  "author": "HaoLe Zheng",
6
6
  "license": "MIT",
@@ -9,7 +9,12 @@
9
9
  "format",
10
10
  "时间格式化"
11
11
  ],
12
- "main": "dist/datex.min.js",
12
+ "main": "./dist/datex.min.js",
13
+ "module": "./dist/datex.min.mjs",
14
+ "exports": {
15
+ "default": "./dist/datex.min.js",
16
+ "import": "./dist/datex.min.mjs"
17
+ },
13
18
  "repository": {
14
19
  "type": "git",
15
20
  "url": "git+https://github.com/mumuy/datex.git"
@@ -18,15 +23,24 @@
18
23
  "url": "https://github.com/mumuy/datex/issues"
19
24
  },
20
25
  "devDependencies": {
26
+ "@babel/core": "^7.23.2",
27
+ "@babel/plugin-syntax-import-attributes": "^7.22.5",
28
+ "@babel/plugin-transform-class-properties": "^7.22.5",
29
+ "@babel/plugin-transform-runtime": "^7.23.2",
30
+ "@babel/preset-env": "^7.23.2",
31
+ "@babel/runtime": "^7.23.2",
32
+ "@rollup/plugin-babel": "^6.0.3",
33
+ "@rollup/plugin-node-resolve": "^15.1.0",
21
34
  "@rollup/plugin-terser": "^0.4.1",
22
- "gulp": "^4.0.2",
23
- "gulp-rename": "^2.0.0",
24
- "rollup": "^3.20.2",
35
+ "acorn-import-assertions": "^1.9.0",
36
+ "rollup": "^3.26.3",
37
+ "rollup-plugin-import-assert": "^3.0.1",
25
38
  "tape": "^5.6.3"
26
39
  },
27
40
  "scripts": {
28
41
  "test": "node test/index.js",
29
- "build": "gulp && npm run test"
42
+ "build": "rollup -c && npm run test",
43
+ "watch": "rollup -wc"
30
44
  },
31
45
  "homepage": "https://passer-by.com/datex/",
32
46
  "directories": {
@@ -0,0 +1,52 @@
1
+ import resolve from '@rollup/plugin-node-resolve'; // 使用node_modules包
2
+ import terser from '@rollup/plugin-terser'; // 代码压缩
3
+ import babel from '@rollup/plugin-babel'; // ECMAScript兼容
4
+ import {importAssertionsPlugin} from 'rollup-plugin-import-assert';
5
+ import {importAssertions} from 'acorn-import-assertions';
6
+ import pkg from './package.json' assert { type:'json' }; // 获取package信息
7
+
8
+ // 版权信息
9
+ const repository = pkg.repository.url.replace(/(.+)(:\/\/.+)\.git$/,'https$2');
10
+ const now = new Date();
11
+ const date = (new Date(now.getTime()-now.getTimezoneOffset()*60000)).toISOString().substr(0,10);
12
+ const banner = `/*!
13
+ * ${pkg.name} v${pkg.version}
14
+ * ${pkg.description}
15
+ * ${pkg.homepage}
16
+ *
17
+ * Copyright (c) 2022-present, ${pkg.author}
18
+ *
19
+ * Released under the ${pkg.license} License
20
+ * ${repository}
21
+ *
22
+ * Created on: ${date}
23
+ */`;
24
+
25
+ const commonPlugins = [
26
+ resolve(),
27
+ importAssertionsPlugin(),
28
+ terser(),
29
+ babel({
30
+ babelHelpers: 'runtime',
31
+ exclude:'node_modules/**'
32
+ })
33
+ ];
34
+
35
+ export default [{
36
+ input: './src/datex.js',
37
+ output:[{
38
+ file: pkg.main,
39
+ format: 'umd',
40
+ name: 'datex',
41
+ banner
42
+ },{
43
+ file: pkg.module,
44
+ format: 'es',
45
+ banner
46
+ }],
47
+ acornInjectPlugins: [ importAssertions ],
48
+ plugins: commonPlugins,
49
+ watch: {
50
+ exclude: 'node_modules/**'
51
+ }
52
+ }];
package/src/datex.js CHANGED
@@ -1,340 +1,17 @@
1
- let _langMap = {};
2
- _langMap['en-us'] = {
3
- 'MMM':['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sept.','Oct.','Nov.','Dec.'],
4
- 'MMMM':['January','February','March','April','May','June','July','August','September','October','November','December'],
5
- 'Do':['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th','13th','14th','15th','16th','17th','18th','19th','20th','21st','22nd','23rd','24th','25th','26th','27th','28th','29th','30th','31st'],
6
- 'WW':['Sun.','Mon.','Tues.','Wed.','Thur.','Fri.','Sat.'],
7
- 'WWW':['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
8
- };
9
- _langMap['zh-cn'] = {
10
- 'MMM':['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
11
- 'MMMM':['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
12
- 'Do':['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日','27日','28日','29日','30日','31日'],
13
- 'WW':['周日','周一','周二','周三','周四','周五','周六'],
14
- 'WWW':['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
15
- };
16
- let _lang = 'en-us';
17
- if(typeof self!='undefined'&&self.navigator){
18
- _lang = self.navigator.language.toLowerCase();
19
- if (!_langMap[_lang]) {
20
- _lang = 'en-us';
21
- }
22
- }
23
- let _timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
24
- let _offset = 0;
25
- const period = ['year','month','day','hour','minute','second','millsecond'];
26
- const initTime = [1970,1,1,0,0,0,0];
27
- const convertTimeZone = (date, timeZone) => {return new Date(date.toLocaleString('en-US', { timeZone }))};
1
+ import datex from './module/factory';
2
+ import baseLoader from './module/method/base';
3
+ import computeLoader from './module/method/compute';
4
+ import compareLoader from './module/method/compare';
5
+ import languageLoader from './module/method/language';
6
+ import timezoneLoader from './module/method/timezone';
28
7
 
29
- function datex(...argu){
30
- return new datex.prototype.init(...argu);
31
- }
32
- function getInstance(that){
33
- return that instanceof datex?that:datex(that);
34
- }
35
- function isObject(value){
36
- return value != null && (typeof value == 'object' || typeof value == 'function');
37
- }
38
-
39
- datex.setLanguage = function(lang,data={}){
40
- _langMap[lang] = Object.assign(_langMap[lang]||{},data);
41
- return this;
42
- };
43
- datex.switchLanguage = function(lang){
44
- _lang = lang;
45
- return this;
46
- };
47
- datex.now = Date.now;
48
- datex.supportedTimezones = typeof Intl!='undefined'&&Intl.supportedValuesOf?Intl.supportedValuesOf('timeZone'):[];
49
- datex.switchTimezone = function(timezone){
50
- _timezone = timezone;
51
- _offset = convertTimeZone(new Date('1970/1/1'),_timezone).getTime() - (new Date('1970/1/1')).getTime();
52
- };
53
- datex.getTimezone = function(){
54
- return _timezone;
55
- };
56
- datex.getTimezoneOffset = function(){
57
- return (new Date).getTimezoneOffset() - _offset/60000;
58
- };
59
-
60
- datex.prototype = {
61
- _date:null,
62
- _langMap:{},
63
- _lang:null,
64
- _timezone:null,
65
- _offset:0,
66
- init:function(...argu){
67
- if(argu.length){
68
- if(argu[0] instanceof Date){
69
- this._date = argu[0];
70
- }else{
71
- if(Array.isArray(argu[0])){
72
- argu = initTime.map((value,index)=>(argu[0][index]||value));
73
- }else if(isObject(argu[0])){
74
- argu = initTime.map((value,index)=>(argu[0][period[index]]||value));
75
- }
76
- if(argu.length==1&&typeof argu[0]=='string'){
77
- let matchs1 = argu[0].match(/(\d{1,4})[\-\/](\d{1,2})[\-\/](\d{1,2})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/);
78
- let matchs2 = argu[0].match(/(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{3,4})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/);
79
- let matchs3 = argu[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);
80
- if(matchs1&&!matchs2){
81
- argu = [1,2,3,5,6,8,10].map(function(i,index){
82
- return +(matchs1[i]||initTime[index]);
83
- });
84
- }else if(matchs2){
85
- argu = [3,1,2,5,6,8,10].map(function(i,index){
86
- return +(matchs2[i]||initTime[index]);
87
- });
88
- }else if(matchs3){
89
- argu = [1,2,3,4,5,6,7].map(function(i,index){
90
- return +(matchs3[i]||initTime[index]);
91
- });
92
- }
93
- }
94
- if(argu.length>=3){
95
- argu[1]--;
96
- }
97
- this._date = new Date(...argu);
98
- if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
99
- this._date.setFullYear(argu[0]);
100
- }
101
- }
102
- if(_offset){
103
- this._date.setTime(this._date.getTime()-_offset);
104
- }
105
- }else{
106
- this._date = new Date();
107
- }
108
- return this;
109
- },
110
- getTime(){
111
- return this._date.getTime();
112
- },
113
- getUnix(){
114
- return ~~(this._date.getTime()/1000);
115
- },
116
- clone(){
117
- return datex(this.getTime());
118
- },
119
- toDate(){
120
- return this._date;
121
- },
122
- toObject(){
123
- let _ = this._date;
124
- return {
125
- 'year':_.getFullYear(),
126
- 'month':_.getMonth()+1,
127
- 'day':_.getDate(),
128
- 'hour':_.getHours(),
129
- 'minute':_.getMinutes(),
130
- 'second':_.getSeconds(),
131
- 'millsecond':_.getMilliseconds(),
132
- 'timestamp':_.getTime(),
133
- 'week':_.getDay()
134
- };
135
- },
136
- toArray(){
137
- let $ = this.toObject();
138
- return period.map(name=>$[name]);
139
- },
140
- toString(){
141
- return this._date.toString();
142
- },
143
- toISOString(){
144
- return this._date.toISOString();
145
- },
146
- set(unit,value){
147
- let _ = this._date;
148
- let $ = this.toObject();
149
- switch (unit) {
150
- case 'year':
151
- _.setFullYear(value);
152
- break;
153
- case 'month':
154
- _.setMonth(value-1);
155
- break;
156
- case 'day':
157
- _.setDate(value);
158
- break;
159
- case 'hour':
160
- _.setHours(value);
161
- break;
162
- case 'minute':
163
- _.setMinutes(value);
164
- break;
165
- case 'second':
166
- _.setSeconds(value);
167
- break;
168
- case 'millsecond':
169
- _.setMilliseconds(value);
170
- break;
171
- case 'timestamp':
172
- _.setTime(value);
173
- break;
174
- case 'week':
175
- _.setDate($.day-$.week+value);
176
- break;
177
- }
178
- return this;
179
- },
180
- get(unit){
181
- let $ = this.toObject();
182
- return $[unit];
183
- },
184
- change(unit,value){
185
- let $ = this.toObject();
186
- return this.set(unit,$[unit]+value);
187
- },
188
- format(pattern = 'YYYY-MM-DD HH:mm:ss'){
189
- let that = this.clone();
190
- let offset = this._offset||_offset;
191
- that._date.setTime(this._date.getTime()+offset);
192
- let _ = that._date;
193
- let $ = that.toObject();
194
- let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
195
- let map = {
196
- 'YYYY':''+$.year,
197
- 'YY':(''+$.year).padStart(2,'0'),
198
- 'MM':(''+$.month).padStart(2,'0'),
199
- 'M':''+$.month,
200
- 'DD':(''+$.day).padStart(2,'0'),
201
- 'D':''+$.day,
202
- 'HH':(''+$.hour).padStart(2,'0'),
203
- 'H':''+$.hour,
204
- 'hh':(''+($.hour%12)).padStart(2,'0'),
205
- 'h':''+($.hour%12),
206
- 'mm':(''+$.minute).padStart(2,'0'),
207
- 'm':''+$.minute,
208
- 'ss':(''+$.second).padStart(2,'0'),
209
- 's':''+$.second,
210
- 'S':''+(~~(($.millsecond%1000)/100)),
211
- 'SS':''+(~~(($.millsecond%1000)/10)),
212
- 'SSS':''+($.millsecond%1000),
213
- 'Z':match[1]+match[2]+':'+match[3],
214
- 'ZZ':match[1]+match[2]+match[3],
215
- 'A':['AM','PM'][~~($.hour/12)],
216
- 'a':['am','pm'][~~($.hour/12)],
217
- 'X':$.timestamp,
218
- 'x':~~($.timestamp/1000),
219
- 'Q':''+(~~($.month/3)),
220
- 'W':$.week
221
- };
222
- let langMap = Object.assign({},_langMap,this._langMap);
223
- let language = langMap[this._lang||_lang];
224
- map['MMM'] = language['MMM'][$.month-1];
225
- map['MMMM'] = language['MMMM'][$.month-1];
226
- map['Do'] = language['Do'][$.day-1];
227
- map['WW'] = language['WW'][$.week];
228
- map['WWW'] = language['WWW'][$.week];
229
- return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,function(key){
230
- return map[key]||'';
231
- });
232
- },
233
- startOf(unit){
234
- let $ = this.toObject();
235
- let that = null;
236
- let index = period.indexOf(unit)+1;
237
- let dateSet = this.toArray();
238
- let initSet = initTime.slice(index);
239
- dateSet.splice(index,initSet.length,...initSet);
240
- if(unit=='timestamp'){
241
- that = this.clone();
242
- }else if(unit=='week'){
243
- that = datex($.year,$.month,$.day-$.week,0,0,0,0);
244
- }else{
245
- that = datex(...dateSet);
246
- }
247
- return that;
248
- },
249
- endOf(unit){
250
- return this.startOf(unit).change(unit,unit=='week'?7:1).change('millsecond',-1);
251
- },
252
- diffWith(that,unit){
253
- that = getInstance(that);
254
- if(!that.isValid()){
255
- return false;
256
- }
257
- let diffMap = {
258
- 'day':8.64e7,
259
- 'hour':3.6e6,
260
- 'minute':6e4,
261
- 'second':1000,
262
- 'millsecond':1
263
- };
264
- let timestamp = this.getTime()-that.getTime();
265
- let value = 0;
266
- if(unit){
267
- if(diffMap[unit]){
268
- value = ~~(timestamp/diffMap[unit]);
269
- }else if(unit=='month'){
270
- let this_month = 12*(this.get('year')-1)+this.get('month');
271
- let that_month = 12*(that.get('year')-1)+that.get('month');
272
- value = this_month -that_month;
273
- if(value<0&&this.get('day')>that.get('day')){
274
- value+=1;
275
- }else if(value>0&&this.get('day')<that.get('day')){
276
- value-=1;
277
- }
278
- }else if(unit=='year'){
279
- value = this.get('year') - that.get('year');
280
- if(value<0&&(this.get('month')>that.get('month')||this.get('month')==that.get('month')&&this.get('day')>that.get('day'))){
281
- value+=1;
282
- }else if(value>0&&(this.get('month')<that.get('month')||this.get('month')==that.get('month')&&this.get('day')<that.get('day'))){
283
- value-=1;
284
- }
285
- }
286
- return value;
287
- }else{
288
- let clone = this.clone();
289
- let hash = {};
290
- period.forEach(function(unit){
291
- hash[unit] = clone.diffWith(that,unit);
292
- clone.set(unit,that.get(unit));
293
- });
294
- return hash;
295
- }
296
- },
297
- isBefore(that,unit = 'timestamp'){
298
- that = getInstance(that);
299
- return this.get(unit)<that.get(unit);
300
- },
301
- isAfter(that,unit = 'timestamp'){
302
- that = getInstance(that);
303
- return this.get(unit)>that.get(unit);
304
- },
305
- isSame(that,unit = 'timestamp'){
306
- that = getInstance(that);
307
- return this.get(unit)==that.get(unit);
308
- },
309
- isBetween(startDate,endDate,unit = 'timestamp'){
310
- startDate = getInstance(startDate);
311
- endDate = getInstance(endDate);
312
- return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
313
- },
314
- setLanguage(lang,data={}){
315
- lang = lang.toLowerCase();
316
- this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
317
- return this;
318
- },
319
- switchLanguage(lang){
320
- this._lang = lang.toLowerCase();
321
- return this;
322
- },
323
- switchTimezone(timezone){
324
- this._timezone = timezone;
325
- this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
326
- return this;
327
- },
328
- getTimezone(){
329
- return this._timezone||_timezone;
330
- },
331
- getTimezoneOffset(){
332
- return this._date.getTimezoneOffset() - (this._offset||_offset)/60000;
333
- },
334
- isValid(){
335
- return !isNaN(this.getTime());
336
- }
337
- };
338
- datex.prototype.init.prototype = datex.prototype;
8
+ // 功能加载
9
+ [
10
+ baseLoader,
11
+ computeLoader,
12
+ compareLoader,
13
+ languageLoader,
14
+ timezoneLoader
15
+ ].forEach(datex.extend);
339
16
 
340
17
  export default datex;
@@ -0,0 +1,20 @@
1
+ import prototype from './prototype';
2
+
3
+ // 构造器
4
+ function datex(...argu){
5
+ return new datex.prototype.init(...argu);
6
+ }
7
+
8
+ // 当前时间
9
+ datex.now = Date.now;
10
+
11
+ // 原型加载方法
12
+ datex.extend = function(loader){
13
+ loader(datex,prototype);
14
+ };
15
+
16
+ // 初始化 - 类似于jQuery
17
+ datex.prototype = prototype;
18
+ prototype.init.prototype = prototype;
19
+
20
+ export default datex;