datex.js 1.0.9 → 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.9
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-05-10
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);let n=Intl.DateTimeFormat().resolvedOptions().timeZone,i=0;const a=["year","month","day","hour","minute","second","millsecond"],s=[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.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.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=s.map(((e,n)=>t[0][n]||e)):null==(e=t[0])||"object"!=typeof e&&"function"!=typeof e||(t=s.map(((e,n)=>t[0][a[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]||s[n])})):n?t=[3,1,2,5,6,8,10].map((function(t,e){return+(n[t]||s[e])})):i&&(t=[1,2,3,4,5,6,7].map((function(t,e){return+(i[t]||s[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},setLanguage(t,e={}){return this._langMap[t]=Object.assign(this._langMap[t]||{},e),this},switchLanguage(t){return this._lang=t,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},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 a.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 a=this.clone(),s=this._offset||i;a._date.setTime(this._date.getTime()+s);let r=a._date,o=a.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},m=Object.assign({},t,this._langMap)[this._lang||e];return d.MMM=m.MMM[o.month-1],d.MMMM=m.MMMM[o.month-1],d.Do=m.Do[o.day-1],d.WW=m.WW[o.week],d.WWW=m.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=a.indexOf(t)+1,r=this.toArray(),h=s.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(),s=0;if(e){if(n[e])s=~~(i/n[e]);else if("month"==e){s=12*(this.get("year")-1)+this.get("month")-(12*(t.get("year")-1)+t.get("month")),s<0&&this.get("day")>t.get("day")?s+=1:s>0&&this.get("day")<t.get("day")&&(s-=1)}else"year"==e&&(s=this.get("year")-t.get("year"),s<0&&(this.get("month")>t.get("month")||this.get("month")==t.get("month")&&this.get("day")>t.get("day"))?s+=1:s>0&&(this.get("month")<t.get("month")||this.get("month")==t.get("month")&&this.get("day")<t.get("day"))&&(s-=1));return s}{let e=this.clone(),n={};return a.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)},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/index.html CHANGED
@@ -179,8 +179,21 @@
179
179
  <span class="subtitle">switchTimezone(timezone)</span>
180
180
  </a>
181
181
  </li>
182
+
182
183
  <li>
183
184
  <a href="#hash25">
185
+ <span class="title">获取支持时区列表</span>
186
+ <span class="subtitle">supportedTimezones</span>
187
+ </a>
188
+ </li>
189
+ <li>
190
+ <a href="#hash26">
191
+ <span class="title">获取当前时区间差(分钟)</span>
192
+ <span class="subtitle">getTimezoneOffset()</span>
193
+ </a>
194
+ </li>
195
+ <li>
196
+ <a href="#hash27">
184
197
  <span class="title">返回当前时间戳(毫秒)</span>
185
198
  <span class="subtitle">now()</span>
186
199
  </a>
@@ -1123,6 +1136,28 @@
1123
1136
  </div>
1124
1137
  </div>
1125
1138
  <div id="hash25" class="box">
1139
+ <div class="c-hd">
1140
+ <p class="title">获取支持时区列表</p>
1141
+ <p class="subtitle">supportedTimezones</p>
1142
+ </div>
1143
+ <div class="c-bd">
1144
+ <p>支持的时区</p>
1145
+ <p>运行:datex.supportedTimezones</p>
1146
+ <p>返回:['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmera', 'Africa/Bamako',...]</p>
1147
+ </div>
1148
+ </div>
1149
+ <div id="hash26" class="box">
1150
+ <div class="c-hd">
1151
+ <p class="title">获取当前时区与UTC的时间差(分钟)</p>
1152
+ <p class="subtitle">getTimezoneOffset()</p>
1153
+ </div>
1154
+ <div class="c-bd">
1155
+ <p>全局设置</p>
1156
+ <p>运行:datex().switchTimezone("Europe/Berlin").getTimezoneOffset()</p>
1157
+ <p>返回:-60</p>
1158
+ </div>
1159
+ </div>
1160
+ <div id="hash27" class="box">
1126
1161
  <div class="c-hd">
1127
1162
  <p class="title">返回当前时间戳(毫秒)</p>
1128
1163
  <p class="subtitle">now()</p>
@@ -1165,28 +1200,6 @@
1165
1200
  $('.mod-menu').headroom({autoHide:false,scrollOffset:10,background:'transparent',autoFixed:false});
1166
1201
  </script>
1167
1202
  <script async defer src="https://cdn.bootcdn.net/ajax/libs/github-buttons/2.21.1/buttons.min.js"></script>
1168
- <script>
1169
- var _hmt = _hmt || [];
1170
- (function() {
1171
- var hm = document.createElement("script");
1172
- hm.src = "https://hm.baidu.com/hm.js?b86879c9cc57dd112fe1f7dd75206b82";
1173
- var s = document.getElementsByTagName("script")[0];
1174
- s.parentNode.insertBefore(hm, s);
1175
- })();
1176
- </script>
1177
- <script>
1178
- (function(){
1179
- var bp = document.createElement('script');
1180
- var curProtocol = window.location.protocol.split(':')[0];
1181
- if (curProtocol === 'https') {
1182
- bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
1183
- }
1184
- else {
1185
- bp.src = 'http://push.zhanzhang.baidu.com/push.js';
1186
- }
1187
- var s = document.getElementsByTagName("script")[0];
1188
- s.parentNode.insertBefore(bp, s);
1189
- })();
1190
- </script>
1203
+ <script type="text/javascript" src="https://passer-by.com/public/script/stat.js"></script>
1191
1204
  </body>
1192
1205
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datex.js",
3
- "version": "1.0.9",
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,333 +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;
19
- }
20
- let _timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
21
- let _offset = 0;
22
- const period = ['year','month','day','hour','minute','second','millsecond'];
23
- const initTime = [1970,1,1,0,0,0,0];
24
- 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';
25
7
 
26
- function datex(...argu){
27
- return new datex.prototype.init(...argu);
28
- }
29
- function getInstance(that){
30
- return that instanceof datex?that:datex(that);
31
- }
32
- function isObject(value){
33
- return value != null && (typeof value == 'object' || typeof value == 'function');
34
- }
35
-
36
- datex.setLanguage = function(lang,data={}){
37
- _langMap[lang] = Object.assign(_langMap[lang]||{},data);
38
- return this;
39
- };
40
- datex.switchLanguage = function(lang){
41
- _lang = lang;
42
- return this;
43
- };
44
- datex.now = Date.now;
45
- datex.switchTimezone = function(timezone){
46
- _timezone = timezone;
47
- _offset = convertTimeZone(new Date('1970/1/1'),_timezone).getTime() - (new Date('1970/1/1')).getTime();
48
- };
49
- datex.getTimezone = function(){
50
- return _timezone;
51
- };
52
- datex.supportedTimezones = Intl.supportedValuesOf('timeZone');
53
-
54
- datex.prototype = {
55
- _date:null,
56
- _langMap:{},
57
- _lang:null,
58
- _timezone:null,
59
- _offset:0,
60
- init:function(...argu){
61
- if(argu.length){
62
- if(argu[0] instanceof Date){
63
- this._date = argu[0];
64
- }else{
65
- if(Array.isArray(argu[0])){
66
- argu = initTime.map((value,index)=>(argu[0][index]||value));
67
- }else if(isObject(argu[0])){
68
- argu = initTime.map((value,index)=>(argu[0][period[index]]||value));
69
- }
70
- if(argu.length==1&&typeof argu[0]=='string'){
71
- 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}))?)?/);
72
- 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}))?)?/);
73
- let matchs3 = argu[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);
74
- if(matchs1&&!matchs2){
75
- argu = [1,2,3,5,6,8,10].map(function(i,index){
76
- return +(matchs1[i]||initTime[index]);
77
- });
78
- }else if(matchs2){
79
- argu = [3,1,2,5,6,8,10].map(function(i,index){
80
- return +(matchs2[i]||initTime[index]);
81
- });
82
- }else if(matchs3){
83
- argu = [1,2,3,4,5,6,7].map(function(i,index){
84
- return +(matchs3[i]||initTime[index]);
85
- });
86
- }
87
- }
88
- if(argu.length>=3){
89
- argu[1]--;
90
- }
91
- this._date = new Date(...argu);
92
- if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
93
- this._date.setFullYear(argu[0]);
94
- }
95
- }
96
- if(_offset){
97
- this._date.setTime(this._date.getTime()-_offset);
98
- }
99
- }else{
100
- this._date = new Date();
101
- }
102
- return this;
103
- },
104
- getTime(){
105
- return this._date.getTime();
106
- },
107
- getUnix(){
108
- return ~~(this._date.getTime()/1000);
109
- },
110
- clone(){
111
- return datex(this.getTime());
112
- },
113
- toDate(){
114
- return this._date;
115
- },
116
- toObject(){
117
- let _ = this._date;
118
- return {
119
- 'year':_.getFullYear(),
120
- 'month':_.getMonth()+1,
121
- 'day':_.getDate(),
122
- 'hour':_.getHours(),
123
- 'minute':_.getMinutes(),
124
- 'second':_.getSeconds(),
125
- 'millsecond':_.getMilliseconds(),
126
- 'timestamp':_.getTime(),
127
- 'week':_.getDay()
128
- };
129
- },
130
- toArray(){
131
- let $ = this.toObject();
132
- return period.map(name=>$[name]);
133
- },
134
- toString(){
135
- return this._date.toString();
136
- },
137
- toISOString(){
138
- return this._date.toISOString();
139
- },
140
- set(unit,value){
141
- let _ = this._date;
142
- let $ = this.toObject();
143
- switch (unit) {
144
- case 'year':
145
- _.setFullYear(value);
146
- break;
147
- case 'month':
148
- _.setMonth(value-1);
149
- break;
150
- case 'day':
151
- _.setDate(value);
152
- break;
153
- case 'hour':
154
- _.setHours(value);
155
- break;
156
- case 'minute':
157
- _.setMinutes(value);
158
- break;
159
- case 'second':
160
- _.setSeconds(value);
161
- break;
162
- case 'millsecond':
163
- _.setMilliseconds(value);
164
- break;
165
- case 'timestamp':
166
- _.setTime(value);
167
- break;
168
- case 'week':
169
- _.setDate($.day-$.week+value);
170
- break;
171
- }
172
- return this;
173
- },
174
- get(unit){
175
- let $ = this.toObject();
176
- return $[unit];
177
- },
178
- change(unit,value){
179
- let $ = this.toObject();
180
- return this.set(unit,$[unit]+value);
181
- },
182
- format(pattern = 'YYYY-MM-DD HH:mm:ss'){
183
- let that = this.clone();
184
- let offset = this._offset||_offset;
185
- that._date.setTime(this._date.getTime()+offset);
186
- let _ = that._date;
187
- let $ = that.toObject();
188
- let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
189
- let map = {
190
- 'YYYY':''+$.year,
191
- 'YY':(''+$.year).padStart(2,'0'),
192
- 'MM':(''+$.month).padStart(2,'0'),
193
- 'M':''+$.month,
194
- 'DD':(''+$.day).padStart(2,'0'),
195
- 'D':''+$.day,
196
- 'HH':(''+$.hour).padStart(2,'0'),
197
- 'H':''+$.hour,
198
- 'hh':(''+($.hour%12)).padStart(2,'0'),
199
- 'h':''+($.hour%12),
200
- 'mm':(''+$.minute).padStart(2,'0'),
201
- 'm':''+$.minute,
202
- 'ss':(''+$.second).padStart(2,'0'),
203
- 's':''+$.second,
204
- 'S':''+(~~(($.millsecond%1000)/100)),
205
- 'SS':''+(~~(($.millsecond%1000)/10)),
206
- 'SSS':''+($.millsecond%1000),
207
- 'Z':match[1]+match[2]+':'+match[3],
208
- 'ZZ':match[1]+match[2]+match[3],
209
- 'A':['AM','PM'][~~($.hour/12)],
210
- 'a':['am','pm'][~~($.hour/12)],
211
- 'X':$.timestamp,
212
- 'x':~~($.timestamp/1000),
213
- 'Q':''+(~~($.month/3)),
214
- 'W':$.week
215
- };
216
- let langMap = Object.assign({},_langMap,this._langMap);
217
- let language = langMap[this._lang||_lang];
218
- map['MMM'] = language['MMM'][$.month-1];
219
- map['MMMM'] = language['MMMM'][$.month-1];
220
- map['Do'] = language['Do'][$.day-1];
221
- map['WW'] = language['WW'][$.week];
222
- map['WWW'] = language['WWW'][$.week];
223
- return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,function(key){
224
- return map[key]||'';
225
- });
226
- },
227
- startOf(unit){
228
- let $ = this.toObject();
229
- let that = null;
230
- let index = period.indexOf(unit)+1;
231
- let dateSet = this.toArray();
232
- let initSet = initTime.slice(index);
233
- dateSet.splice(index,initSet.length,...initSet);
234
- if(unit=='timestamp'){
235
- that = this.clone();
236
- }else if(unit=='week'){
237
- that = datex($.year,$.month,$.day-$.week,0,0,0,0);
238
- }else{
239
- that = datex(...dateSet);
240
- }
241
- return that;
242
- },
243
- endOf(unit){
244
- return this.startOf(unit).change(unit,unit=='week'?7:1).change('millsecond',-1);
245
- },
246
- diffWith(that,unit){
247
- that = getInstance(that);
248
- if(!that.isValid()){
249
- return false;
250
- }
251
- let diffMap = {
252
- 'day':8.64e7,
253
- 'hour':3.6e6,
254
- 'minute':6e4,
255
- 'second':1000,
256
- 'millsecond':1
257
- };
258
- let timestamp = this.getTime()-that.getTime();
259
- let value = 0;
260
- if(unit){
261
- if(diffMap[unit]){
262
- value = ~~(timestamp/diffMap[unit]);
263
- }else if(unit=='month'){
264
- let this_month = 12*(this.get('year')-1)+this.get('month');
265
- let that_month = 12*(that.get('year')-1)+that.get('month');
266
- value = this_month -that_month;
267
- if(value<0&&this.get('day')>that.get('day')){
268
- value+=1;
269
- }else if(value>0&&this.get('day')<that.get('day')){
270
- value-=1;
271
- }
272
- }else if(unit=='year'){
273
- value = this.get('year') - that.get('year');
274
- if(value<0&&(this.get('month')>that.get('month')||this.get('month')==that.get('month')&&this.get('day')>that.get('day'))){
275
- value+=1;
276
- }else if(value>0&&(this.get('month')<that.get('month')||this.get('month')==that.get('month')&&this.get('day')<that.get('day'))){
277
- value-=1;
278
- }
279
- }
280
- return value;
281
- }else{
282
- let clone = this.clone();
283
- let hash = {};
284
- period.forEach(function(unit){
285
- hash[unit] = clone.diffWith(that,unit);
286
- clone.set(unit,that.get(unit));
287
- });
288
- return hash;
289
- }
290
- },
291
- isBefore(that,unit = 'timestamp'){
292
- that = getInstance(that);
293
- return this.get(unit)<that.get(unit);
294
- },
295
- isAfter(that,unit = 'timestamp'){
296
- that = getInstance(that);
297
- return this.get(unit)>that.get(unit);
298
- },
299
- isSame(that,unit = 'timestamp'){
300
- that = getInstance(that);
301
- return this.get(unit)==that.get(unit);
302
- },
303
- isBetween(startDate,endDate,unit = 'timestamp'){
304
- startDate = getInstance(startDate);
305
- endDate = getInstance(endDate);
306
- return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
307
- },
308
- setLanguage(lang,data={}){
309
- this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
310
- return this;
311
- },
312
- switchLanguage(lang){
313
- this._lang = lang;
314
- return this;
315
- },
316
- switchTimezone(timezone){
317
- this._timezone = timezone;
318
- this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
319
- return this;
320
- },
321
- getTimezone(){
322
- return this._timezone||_timezone;
323
- },
324
- getTimezoneOffset(){
325
- return this.getTimezone() - (this._offset||_offset)/60000;
326
- },
327
- isValid(){
328
- return !isNaN(this.getTime());
329
- }
330
- };
331
- datex.prototype.init.prototype = datex.prototype;
8
+ // 功能加载
9
+ [
10
+ baseLoader,
11
+ computeLoader,
12
+ compareLoader,
13
+ languageLoader,
14
+ timezoneLoader
15
+ ].forEach(datex.extend);
332
16
 
333
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
+ };
@@ -1,54 +1,54 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <svg width="1440px" height="448px" viewBox="0 0 1440 448" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
- <!-- Generator: Sketch 59.1 (86144) - https://sketch.com -->
4
- <title>编组 5备份</title>
5
- <desc>Created with Sketch.</desc>
6
- <defs>
7
- <linearGradient x1="-5.68700053%" y1="45.5298642%" x2="84.7892757%" y2="53.934985%" id="linearGradient-1">
8
- <stop stop-color="#FBFCFD" offset="0%"></stop>
9
- <stop stop-color="#F8FCFF" offset="100%"></stop>
10
- </linearGradient>
11
- <radialGradient cx="49.4552285%" cy="50%" fx="49.4552285%" fy="50%" r="94.8348304%" gradientTransform="translate(0.494552,0.500000),scale(0.311111,1.000000),rotate(90.000000),translate(-0.494552,-0.500000)" id="radialGradient-2">
12
- <stop stop-color="#FFFFFF" stop-opacity="0.5" offset="0%"></stop>
13
- <stop stop-color="#EDF6FF" stop-opacity="0.578179633" offset="100%"></stop>
14
- </radialGradient>
15
- <rect id="path-3" x="0" y="0" width="1440" height="448"></rect>
16
- <linearGradient x1="72.8463444%" y1="12.5451885%" x2="72.8463444%" y2="295.836589%" id="linearGradient-5">
17
- <stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
18
- <stop stop-color="#9FD7FF" stop-opacity="0.383058348" offset="100%"></stop>
19
- </linearGradient>
20
- <linearGradient x1="16.6159843%" y1="49.1386719%" x2="5.85340543%" y2="50.8613281%" id="linearGradient-6">
21
- <stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
22
- <stop stop-color="#F2F7FC" offset="100%"></stop>
23
- </linearGradient>
24
- <rect id="path-7" x="0" y="259" width="1440" height="189"></rect>
25
- <linearGradient x1="54.7550093%" y1="16.6478641%" x2="54.7550093%" y2="112.331979%" id="linearGradient-8">
26
- <stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
27
- <stop stop-color="#D2ECFF" stop-opacity="0.225387893" offset="100%"></stop>
28
- </linearGradient>
29
- </defs>
30
- <g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
31
- <g id="导航方案备份">
32
- <g id="编组-5备份">
33
- <g id="路径备份">
34
- <g id="蒙版">
35
- <g transform="translate(0.000000, 0.000000)">
36
- <mask id="mask-4" fill="white">
37
- <use xlink:href="#path-3"></use>
38
- </mask>
39
- <g>
40
- <use fill="url(#linearGradient-1)" xlink:href="#path-3"></use>
41
- <use fill="url(#radialGradient-2)" xlink:href="#path-3"></use>
42
- </g>
43
- <g id="矩形备份-32" mask="url(#mask-4)">
44
- <use fill="url(#linearGradient-5)" xlink:href="#path-7"></use>
45
- <use fill="url(#linearGradient-6)" xlink:href="#path-7"></use>
46
- </g>
47
- <rect id="矩形" fill="url(#linearGradient-8)" mask="url(#mask-4)" x="0" y="166" width="1440" height="282"></rect>
48
- </g>
49
- </g>
50
- </g>
51
- </g>
52
- </g>
53
- </g>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <svg width="1440px" height="448px" viewBox="0 0 1440 448" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
3
+ <!-- Generator: Sketch 59.1 (86144) - https://sketch.com -->
4
+ <title>编组 5备份</title>
5
+ <desc>Created with Sketch.</desc>
6
+ <defs>
7
+ <linearGradient x1="-5.68700053%" y1="45.5298642%" x2="84.7892757%" y2="53.934985%" id="linearGradient-1">
8
+ <stop stop-color="#FBFCFD" offset="0%"></stop>
9
+ <stop stop-color="#F8FCFF" offset="100%"></stop>
10
+ </linearGradient>
11
+ <radialGradient cx="49.4552285%" cy="50%" fx="49.4552285%" fy="50%" r="94.8348304%" gradientTransform="translate(0.494552,0.500000),scale(0.311111,1.000000),rotate(90.000000),translate(-0.494552,-0.500000)" id="radialGradient-2">
12
+ <stop stop-color="#FFFFFF" stop-opacity="0.5" offset="0%"></stop>
13
+ <stop stop-color="#EDF6FF" stop-opacity="0.578179633" offset="100%"></stop>
14
+ </radialGradient>
15
+ <rect id="path-3" x="0" y="0" width="1440" height="448"></rect>
16
+ <linearGradient x1="72.8463444%" y1="12.5451885%" x2="72.8463444%" y2="295.836589%" id="linearGradient-5">
17
+ <stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
18
+ <stop stop-color="#9FD7FF" stop-opacity="0.383058348" offset="100%"></stop>
19
+ </linearGradient>
20
+ <linearGradient x1="16.6159843%" y1="49.1386719%" x2="5.85340543%" y2="50.8613281%" id="linearGradient-6">
21
+ <stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
22
+ <stop stop-color="#F2F7FC" offset="100%"></stop>
23
+ </linearGradient>
24
+ <rect id="path-7" x="0" y="259" width="1440" height="189"></rect>
25
+ <linearGradient x1="54.7550093%" y1="16.6478641%" x2="54.7550093%" y2="112.331979%" id="linearGradient-8">
26
+ <stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
27
+ <stop stop-color="#D2ECFF" stop-opacity="0.225387893" offset="100%"></stop>
28
+ </linearGradient>
29
+ </defs>
30
+ <g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
31
+ <g id="导航方案备份">
32
+ <g id="编组-5备份">
33
+ <g id="路径备份">
34
+ <g id="蒙版">
35
+ <g transform="translate(0.000000, 0.000000)">
36
+ <mask id="mask-4" fill="white">
37
+ <use xlink:href="#path-3"></use>
38
+ </mask>
39
+ <g>
40
+ <use fill="url(#linearGradient-1)" xlink:href="#path-3"></use>
41
+ <use fill="url(#radialGradient-2)" xlink:href="#path-3"></use>
42
+ </g>
43
+ <g id="矩形备份-32" mask="url(#mask-4)">
44
+ <use fill="url(#linearGradient-5)" xlink:href="#path-7"></use>
45
+ <use fill="url(#linearGradient-6)" xlink:href="#path-7"></use>
46
+ </g>
47
+ <rect id="矩形" fill="url(#linearGradient-8)" mask="url(#mask-4)" x="0" y="166" width="1440" height="282"></rect>
48
+ </g>
49
+ </g>
50
+ </g>
51
+ </g>
52
+ </g>
53
+ </g>
54
54
  </svg>
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();