datex.js 1.0.10 → 1.0.11

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.
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-08-03
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";const t=["year","month","day","hour","minute","second","millsecond"],e=[1970,1,1,0,0,0,0],n={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1};let i=[];var s={_date:null,init:function(...n){if(n.length){if(n[0].__proto__==this.__proto__)return n[0];if(n[0]instanceof Date)this._date=n[0];else{if(Array.isArray(n[0])?n=e.map(((t,e)=>n[0][e]||t)):null==(s=n[0])||"object"!=typeof s&&"function"!=typeof s||(n=e.map(((e,i)=>n[0][t[i]]||e))),1==n.length&&"string"==typeof n[0]){let t=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}))?)?/),i=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}))?)?/),s=n[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);t&&!i?n=[1,2,3,5,6,8,10].map((function(n,i){return+(t[n]||e[i])})):i?n=[3,1,2,5,6,8,10].map((function(t,n){return+(i[t]||e[n])})):s&&(n=[1,2,3,4,5,6,7].map((function(t,n){return+(s[t]||e[n])})))}n.length>=3&&n[1]--,this._date=new Date(...n),n.length>=2&&!isNaN(n[0])&&n[0]<100&&this._date.setFullYear(n[0])}}else this._date=new Date;var s;let a=this;return i.forEach((function(t){t.bind(a)(...n)})),this},onInit:function(t){i.push(t)}};function a(...t){return new a.prototype.init(...t)}a.now=Date.now,a.extend=function(t){t(a,s)},a.prototype=s,s.init.prototype=s;var o={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"]},r={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(e,i){Object.assign(i,{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 e=this.toObject();return t.map((t=>e[t]))},toString(){return this._date.toString()},toISOString(){return this._date.toISOString()},getTime(){return this._date.getTime()},getUnix(){return~~(this._date.getTime()/1e3)},clone(){let t=this,n=e(this.getTime());return Object.getOwnPropertyNames(t).forEach((function(e){"_date"!=e&&(n[e]=t[e])})),n},isValid(){return!isNaN(this.getTime())},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},change(t,e){let i=this.toObject();return void 0!==n[t]?this.set("timestamp",i.timestamp+e*n[t]):this.set(t,i[t]+e)},get(t){return this.toObject()[t]},format(t="YYYY-MM-DD HH:mm:ss"){let e=this.clone(),n=e._date,i=e.toObject(),s=n.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),a={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:s[1]+s[2]+":"+s[3],ZZ:s[1]+s[2]+s[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 a[t]||""}))}})},function(i,s){Object.assign(s,{diffWith(e,s){if(!(e=i(e)).isValid())return!1;let a=this.getTime()-e.getTime(),o=0;if(s){if(n[s])o=~~(a/n[s]);else if("month"==s){o=12*(this.get("year")-1)+this.get("month")-(12*(e.get("year")-1)+e.get("month")),o<0&&this.get("day")>e.get("day")?o+=1:o>0&&this.get("day")<e.get("day")&&(o-=1)}else"year"==s&&(o=this.get("year")-e.get("year"),o<0&&(this.get("month")>e.get("month")||this.get("month")==e.get("month")&&this.get("day")>e.get("day"))?o+=1:o>0&&(this.get("month")<e.get("month")||this.get("month")==e.get("month")&&this.get("day")<e.get("day"))&&(o-=1));return o}{let n=this.clone(),i={};return t.forEach((function(t){i[t]=n.diffWith(e,t),n.set(t,e.get(t))})),i}},endOf(t){return this.startOf(t).change(t,"week"==t?7:1).change("millsecond",-1)},startOf(n){let s=this.toObject(),a=null,o=t.indexOf(n)+1,r=this.toArray(),h=e.slice(o);return r.splice(o,h.length,...h),a="timestamp"==n?this.clone():"week"==n?i(s.year,s.month,s.day-s.week,0,0,0,0):i(...r),a}})},function(t,e){Object.assign(e,{isAfter(e,n="timestamp"){return e=t(e),this.get(n)>e.get(n)},isBefore(e,n="timestamp"){return e=t(e),this.get(n)<e.get(n)},isBetween(e,n,i="timestamp"){return e=t(e),n=t(n),this.get(i)>e.get(i)&&this.get(i)<n.get(i)},isSame(e,n="timestamp"){return e=t(e),this.get(n)==e.get(n)}})},function(t,e){let n={};n["en-us"]=o,n["zh-cn"]=r;let i="en-us";"undefined"!=typeof self&&self.navigator&&(i=self.navigator.language.toLowerCase(),n[i]||(i="en-us")),Object.assign(t,{setLanguage(t,e={}){return t=t.toLowerCase(),n[t]=Object.assign(n[t]||{},e),this},switchLanguage(t){return i=t.toLowerCase(),this},getLanguage:()=>n[i]}),Object.assign(e,{_langMap:{},_lang:null,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},getLanguage(){return this._langMap[this._lang]}}),e.onInit((function(){this._langMap=Object.assign(this._langMap,n),this._lang=i}));let s=e.format;Object.assign(e,{format(e="YYYY-MM-DD HH:mm:ss"){let n=this.clone().toObject(),i=t.getLanguage()||this.getLanguage(),a={};a.MMM=i.MMM[n.month-1],a.MMMM=i.MMMM[n.month-1],a.Do=i.Do[n.day-1],a.WW=i.WW[n.week],a.WWW=i.WWW[n.week];for(let t in a)e=e.replace(t,a[t]||"");return s.bind(this)(e)}})},function(t,e){let n=Intl.DateTimeFormat().resolvedOptions().timeZone,i=0;const s=(t,e)=>new Date(t.toLocaleString("en-US",{timeZone:e}));Object.assign(t,{supportedTimezones:"undefined"!=typeof Intl&&Intl.supportedValuesOf?Intl.supportedValuesOf("timeZone"):[],switchTimezone(t){return n=t,i=s(new Date("1970/1/1"),n).getTime()-new Date("1970/1/1").getTime(),this},getTimezoneOffset:()=>(new Date).getTimezoneOffset()-i/6e4,getTimezone:()=>n}),Object.assign(e,{_timezone:null,_offset:0,switchTimezone(t){return this._timezone=t,this._offset=s(new Date("1970/1/1"),this._timezone).getTime()-new Date("1970/1/1").getTime(),this},getTimezoneOffset(){return this._date.getTimezoneOffset()-this._offset/6e4},getTimezone(){return this._timezone}}),e.onInit((function(...e){this._timezone=n,this._offset=i,e.length&&(e[0]instanceof Date||e[0]instanceof t||1==e.length&&"number"==typeof e[0]||i&&this._date.setTime(this._date.getTime()-i))}));let a=e.format;Object.assign(e,{format(t="YYYY-MM-DD HH:mm:ss"){let e=this.clone();return e._date.setTime(e._date.getTime()+e._offset),a.bind(e)(t)}})}].forEach(a.extend),a}));
package/gulpfile.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const gulp = require('gulp');
2
- const rename = require("gulp-rename");
3
2
  const rollup = require('rollup');
4
3
  const terser = require('@rollup/plugin-terser');
5
4
  const pkg = require('./package');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datex.js",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "a datetime format library",
5
5
  "author": "HaoLe Zheng",
6
6
  "license": "MIT",
@@ -20,7 +20,6 @@
20
20
  "devDependencies": {
21
21
  "@rollup/plugin-terser": "^0.4.1",
22
22
  "gulp": "^4.0.2",
23
- "gulp-rename": "^2.0.0",
24
23
  "rollup": "^3.20.2",
25
24
  "tape": "^5.6.3"
26
25
  },
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;
@@ -0,0 +1,135 @@
1
+ import {periodKey,periodTime} from './map/period';
2
+
3
+ export default function(datex,proto){
4
+
5
+ Object.assign(proto,{
6
+ toDate(){
7
+ return this._date;
8
+ },
9
+ toObject(){
10
+ let _ = this._date;
11
+ return {
12
+ 'year':_.getFullYear(),
13
+ 'month':_.getMonth()+1,
14
+ 'day':_.getDate(),
15
+ 'hour':_.getHours(),
16
+ 'minute':_.getMinutes(),
17
+ 'second':_.getSeconds(),
18
+ 'millsecond':_.getMilliseconds(),
19
+ 'timestamp':_.getTime(),
20
+ 'week':_.getDay()
21
+ };
22
+ },
23
+ toArray(){
24
+ let $ = this.toObject();
25
+ return periodKey.map(name=>$[name]);
26
+ },
27
+ toString(){
28
+ return this._date.toString();
29
+ },
30
+ toISOString(){
31
+ return this._date.toISOString();
32
+ },
33
+ getTime(){
34
+ return this._date.getTime();
35
+ },
36
+ getUnix(){
37
+ return ~~(this._date.getTime()/1000);
38
+ },
39
+ clone(){
40
+ let that = this;
41
+ let clone = datex(this.getTime());
42
+ Object.getOwnPropertyNames(that).forEach(function(name){
43
+ if(name!='_date'){
44
+ clone[name] = that[name];
45
+ }
46
+ });
47
+ return clone;
48
+ },
49
+ isValid(){
50
+ return !isNaN(this.getTime());
51
+ },
52
+ set(unit,value){
53
+ let _ = this._date;
54
+ let $ = this.toObject();
55
+ switch (unit) {
56
+ case 'year':
57
+ _.setFullYear(value);
58
+ break;
59
+ case 'month':
60
+ _.setMonth(value-1);
61
+ break;
62
+ case 'day':
63
+ _.setDate(value);
64
+ break;
65
+ case 'hour':
66
+ _.setHours(value);
67
+ break;
68
+ case 'minute':
69
+ _.setMinutes(value);
70
+ break;
71
+ case 'second':
72
+ _.setSeconds(value);
73
+ break;
74
+ case 'millsecond':
75
+ _.setMilliseconds(value);
76
+ break;
77
+ case 'timestamp':
78
+ _.setTime(value);
79
+ break;
80
+ case 'week':
81
+ _.setDate($.day-$.week+value);
82
+ break;
83
+ }
84
+ return this;
85
+ },
86
+ change(unit,value){
87
+ let $ = this.toObject();
88
+ if(typeof periodTime[unit]!='undefined'){
89
+ return this.set('timestamp',$['timestamp']+value*periodTime[unit]);
90
+ }else{
91
+ return this.set(unit,$[unit]+value);
92
+ }
93
+ },
94
+ get(unit){
95
+ let $ = this.toObject();
96
+ return $[unit];
97
+ },
98
+ format(pattern = 'YYYY-MM-DD HH:mm:ss'){
99
+ let that = this.clone();
100
+ let _ = that._date;
101
+ let $ = that.toObject();
102
+ let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
103
+ let map = {
104
+ 'YYYY':''+$.year,
105
+ 'YY':(''+$.year).padStart(2,'0'),
106
+ 'MM':(''+$.month).padStart(2,'0'),
107
+ 'M':''+$.month,
108
+ 'DD':(''+$.day).padStart(2,'0'),
109
+ 'D':''+$.day,
110
+ 'HH':(''+$.hour).padStart(2,'0'),
111
+ 'H':''+$.hour,
112
+ 'hh':(''+($.hour%12)).padStart(2,'0'),
113
+ 'h':''+($.hour%12),
114
+ 'mm':(''+$.minute).padStart(2,'0'),
115
+ 'm':''+$.minute,
116
+ 'ss':(''+$.second).padStart(2,'0'),
117
+ 's':''+$.second,
118
+ 'S':''+(~~(($.millsecond%1000)/100)),
119
+ 'SS':''+(~~(($.millsecond%1000)/10)),
120
+ 'SSS':''+($.millsecond%1000),
121
+ 'Z':match[1]+match[2]+':'+match[3],
122
+ 'ZZ':match[1]+match[2]+match[3],
123
+ 'A':['AM','PM'][~~($.hour/12)],
124
+ 'a':['am','pm'][~~($.hour/12)],
125
+ 'X':$.timestamp,
126
+ 'x':~~($.timestamp/1000),
127
+ 'Q':''+(~~($.month/3)),
128
+ 'W':$.week
129
+ };
130
+ return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|A|a|X|x|Q|W+/g,function(key){
131
+ return map[key]||'';
132
+ });
133
+ }
134
+ });
135
+ };
@@ -0,0 +1,22 @@
1
+ export default function(datex,proto){
2
+
3
+ Object.assign(proto,{
4
+ isAfter(that,unit = 'timestamp'){
5
+ that = datex(that);
6
+ return this.get(unit)>that.get(unit);
7
+ },
8
+ isBefore(that,unit = 'timestamp'){
9
+ that = datex(that);
10
+ return this.get(unit)<that.get(unit);
11
+ },
12
+ isBetween(startDate,endDate,unit = 'timestamp'){
13
+ startDate = datex(startDate);
14
+ endDate = datex(endDate);
15
+ return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
16
+ },
17
+ isSame(that,unit = 'timestamp'){
18
+ that = datex(that);
19
+ return this.get(unit)==that.get(unit);
20
+ }
21
+ });
22
+ };
@@ -0,0 +1,64 @@
1
+ import {periodKey,periodValue,periodTime} from './map/period';
2
+
3
+ export default function(datex,proto){
4
+
5
+ Object.assign(proto,{
6
+ diffWith(that,unit){
7
+ that = datex(that);
8
+ if(!that.isValid()){
9
+ return false;
10
+ }
11
+ let timestamp = this.getTime()-that.getTime();
12
+ let value = 0;
13
+ if(unit){
14
+ if(periodTime[unit]){
15
+ value = ~~(timestamp/periodTime[unit]);
16
+ }else if(unit=='month'){
17
+ let this_month = 12*(this.get('year')-1)+this.get('month');
18
+ let that_month = 12*(that.get('year')-1)+that.get('month');
19
+ value = this_month -that_month;
20
+ if(value<0&&this.get('day')>that.get('day')){
21
+ value+=1;
22
+ }else if(value>0&&this.get('day')<that.get('day')){
23
+ value-=1;
24
+ }
25
+ }else if(unit=='year'){
26
+ value = this.get('year') - that.get('year');
27
+ if(value<0&&(this.get('month')>that.get('month')||this.get('month')==that.get('month')&&this.get('day')>that.get('day'))){
28
+ value+=1;
29
+ }else if(value>0&&(this.get('month')<that.get('month')||this.get('month')==that.get('month')&&this.get('day')<that.get('day'))){
30
+ value-=1;
31
+ }
32
+ }
33
+ return value;
34
+ }else{
35
+ let clone = this.clone();
36
+ let hash = {};
37
+ periodKey.forEach(function(unit){
38
+ hash[unit] = clone.diffWith(that,unit);
39
+ clone.set(unit,that.get(unit));
40
+ });
41
+ return hash;
42
+ }
43
+ },
44
+ endOf(unit){
45
+ return this.startOf(unit).change(unit,unit=='week'?7:1).change('millsecond',-1);
46
+ },
47
+ startOf(unit){
48
+ let $ = this.toObject();
49
+ let that = null;
50
+ let index = periodKey.indexOf(unit)+1;
51
+ let dateSet = this.toArray();
52
+ let initSet = periodValue.slice(index);
53
+ dateSet.splice(index,initSet.length,...initSet);
54
+ if(unit=='timestamp'){
55
+ that = this.clone();
56
+ }else if(unit=='week'){
57
+ that = datex($.year,$.month,$.day-$.week,0,0,0,0);
58
+ }else{
59
+ that = datex(...dateSet);
60
+ }
61
+ return that;
62
+ }
63
+ });
64
+ };
@@ -0,0 +1,72 @@
1
+ import en_us from './locale/en-us';
2
+ import zh_cn from './locale/zh-cn';
3
+
4
+ export default function(datex,proto){
5
+ let _langMap = {};
6
+ _langMap['en-us'] = en_us;
7
+ _langMap['zh-cn'] = zh_cn;
8
+ let _lang = 'en-us';
9
+ if(typeof self!='undefined'&&self.navigator){
10
+ _lang = self.navigator.language.toLowerCase();
11
+ if (!_langMap[_lang]) {
12
+ _lang = 'en-us';
13
+ }
14
+ }
15
+
16
+ Object.assign(datex,{
17
+ setLanguage(lang,data={}){
18
+ lang = lang.toLowerCase();
19
+ _langMap[lang] = Object.assign(_langMap[lang]||{},data);
20
+ return this;
21
+ },
22
+ switchLanguage(lang){
23
+ _lang = lang.toLowerCase();
24
+ return this;
25
+ },
26
+ getLanguage(){
27
+ return _langMap[_lang];
28
+ }
29
+ });
30
+
31
+ Object.assign(proto,{
32
+ _langMap:{},
33
+ _lang:null,
34
+ setLanguage(lang,data={}){
35
+ lang = lang.toLowerCase();
36
+ this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
37
+ return this;
38
+ },
39
+ switchLanguage(lang){
40
+ this._lang = lang.toLowerCase();
41
+ return this;
42
+ },
43
+ getLanguage(){
44
+ return this._langMap[this._lang];
45
+ }
46
+ });
47
+
48
+ proto.onInit(function(){
49
+ this._langMap = Object.assign(this._langMap,_langMap);
50
+ this._lang = _lang;
51
+ });
52
+
53
+ // 重写
54
+ let format = proto.format;
55
+ Object.assign(proto,{
56
+ format(pattern = 'YYYY-MM-DD HH:mm:ss'){
57
+ let that = this.clone();
58
+ let $ = that.toObject();
59
+ let language = datex.getLanguage()||this.getLanguage();
60
+ let map = {};
61
+ map['MMM'] = language['MMM'][$.month-1];
62
+ map['MMMM'] = language['MMMM'][$.month-1];
63
+ map['Do'] = language['Do'][$.day-1];
64
+ map['WW'] = language['WW'][$.week];
65
+ map['WWW'] = language['WWW'][$.week];
66
+ for (let key in map) {
67
+ pattern = pattern.replace(key,map[key]||'');
68
+ }
69
+ return format.bind(this)(pattern);
70
+ }
71
+ });
72
+ };
@@ -0,0 +1,7 @@
1
+ export default {
2
+ 'MMM':['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sept.','Oct.','Nov.','Dec.'],
3
+ 'MMMM':['January','February','March','April','May','June','July','August','September','October','November','December'],
4
+ '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'],
5
+ 'WW':['Sun.','Mon.','Tues.','Wed.','Thur.','Fri.','Sat.'],
6
+ 'WWW':['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
7
+ };
@@ -0,0 +1,7 @@
1
+ export default {
2
+ 'MMM':['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
3
+ 'MMMM':['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
4
+ '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日'],
5
+ 'WW':['周日','周一','周二','周三','周四','周五','周六'],
6
+ 'WWW':['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
7
+ };
@@ -0,0 +1,9 @@
1
+ export const periodKey = ['year','month','day','hour','minute','second','millsecond'];
2
+ export const periodValue = [1970,1,1,0,0,0,0];
3
+ export const periodTime = {
4
+ 'day':8.64e7,
5
+ 'hour':3.6e6,
6
+ 'minute':6e4,
7
+ 'second':1000,
8
+ 'millsecond':1
9
+ };
@@ -0,0 +1,60 @@
1
+ export default function(datex,proto){
2
+ let _timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
3
+ let _offset = 0;
4
+
5
+ const convertTimeZone = (date, timeZone) => {return new Date(date.toLocaleString('en-US', { timeZone }))};
6
+
7
+ Object.assign(datex,{
8
+ supportedTimezones:(typeof Intl!='undefined'&&Intl.supportedValuesOf?Intl.supportedValuesOf('timeZone'):[]),
9
+ switchTimezone(timeZone){
10
+ _timezone = timeZone;
11
+ _offset = convertTimeZone(new Date('1970/1/1'),_timezone).getTime() - (new Date('1970/1/1')).getTime();
12
+ return this;
13
+ },
14
+ getTimezoneOffset(){
15
+ return (new Date).getTimezoneOffset() - _offset/60000;
16
+ },
17
+ getTimezone(){
18
+ return _timezone;
19
+ }
20
+ });
21
+
22
+ Object.assign(proto,{
23
+ _timezone:null,
24
+ _offset:0,
25
+ switchTimezone(timezone){
26
+ this._timezone = timezone;
27
+ this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
28
+ return this;
29
+ },
30
+ getTimezoneOffset(){
31
+ return this._date.getTimezoneOffset() - this._offset/60000;
32
+ },
33
+ getTimezone(){
34
+ return this._timezone;
35
+ }
36
+ });
37
+
38
+ proto.onInit(function(...argu){
39
+ this._timezone = _timezone;
40
+ this._offset = _offset;
41
+ if(argu.length){
42
+ if(argu[0] instanceof Date){
43
+ }else if(argu[0] instanceof datex){
44
+ }else if(argu.length==1&&typeof argu[0]=='number'){
45
+ }else if(_offset){
46
+ this._date.setTime(this._date.getTime()-_offset);
47
+ }
48
+ }
49
+ });
50
+
51
+ // 重写
52
+ let format = proto.format;
53
+ Object.assign(proto,{
54
+ format(pattern = 'YYYY-MM-DD HH:mm:ss'){
55
+ let that = this.clone();
56
+ that._date.setTime(that._date.getTime()+that._offset);
57
+ return format.bind(that)(pattern);
58
+ }
59
+ });
60
+ };
@@ -0,0 +1,63 @@
1
+ import {periodKey,periodValue} from './method/map/period';
2
+
3
+ function isObject(value){
4
+ return value != null && (typeof value == 'object' || typeof value == 'function');
5
+ }
6
+
7
+ let taskQueue = [];
8
+
9
+ export default {
10
+ _date:null,
11
+ init:function(...argu){
12
+ if(argu.length){
13
+
14
+ if(argu[0].__proto__==this.__proto__){
15
+ return argu[0];
16
+ }else if(argu[0] instanceof Date){
17
+ this._date = argu[0];
18
+ }else{
19
+ if(Array.isArray(argu[0])){
20
+ argu = periodValue.map((value,index)=>(argu[0][index]||value));
21
+ }else if(isObject(argu[0])){
22
+ argu = periodValue.map((value,index)=>(argu[0][periodKey[index]]||value));
23
+ }
24
+ if(argu.length==1&&typeof argu[0]=='string'){
25
+ 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}))?)?/);
26
+ 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}))?)?/);
27
+ let matchs3 = argu[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);
28
+ if(matchs1&&!matchs2){
29
+ argu = [1,2,3,5,6,8,10].map(function(i,index){
30
+ return +(matchs1[i]||periodValue[index]);
31
+ });
32
+ }else if(matchs2){
33
+ argu = [3,1,2,5,6,8,10].map(function(i,index){
34
+ return +(matchs2[i]||periodValue[index]);
35
+ });
36
+ }else if(matchs3){
37
+ argu = [1,2,3,4,5,6,7].map(function(i,index){
38
+ return +(matchs3[i]||periodValue[index]);
39
+ });
40
+ }
41
+ }
42
+ if(argu.length>=3){
43
+ argu[1]--;
44
+ }
45
+ this._date = new Date(...argu);
46
+ if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
47
+ this._date.setFullYear(argu[0]);
48
+ }
49
+ }
50
+ }else{
51
+ this._date = new Date();
52
+ }
53
+
54
+ let _ = this;
55
+ taskQueue.forEach(function(task){
56
+ task.bind(_)(...argu);
57
+ });
58
+ return this;
59
+ },
60
+ onInit:function(callback){
61
+ taskQueue.push(callback);
62
+ }
63
+ };
package/test/index.js CHANGED
@@ -10,6 +10,7 @@ test('[Test]', function (t) {
10
10
  t.deepEqual(datex(2022,10,1).change('year',1).format(),'2023-10-01 00:00:00');
11
11
  t.deepEqual(datex(2022,10,10).startOf('month').format(),'2022-10-01 00:00:00');
12
12
  t.deepEqual(datex(2022,10,10).endOf('month').format(),'2022-10-31 23:59:59');
13
+ t.deepEqual(datex('2008-08-08 20:00').change('hour',0.5).format('HH:mm'),'20:30');
13
14
  t.deepEqual(datex(2008,8,8,23,45,45).get('hour'),23);
14
15
  t.deepEqual(datex('1949-10-01').diffWith('2022-12-01','month'),-878);
15
16
  t.end();