datex.js 1.0.5 → 1.0.8

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,13 +1,13 @@
1
- /*!
2
- * datex.js v1.0.3
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-04-14
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"],W:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],w:["Sun.","Mon.","Tues.","Wed.","Thur.","Fri.","Sat."]},"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日"],W:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],w:["周日","周一","周二","周三","周四","周五","周六"]}},e=t["en-US"];"undefined"!=typeof self&&self.navigator&&(e=t[self.navigator.language]);const n=["year","month","day","hour","minute","second","millsecond"],r=[1970,1,1,0,0,0,0];function a(...t){return new a.prototype.init(...t)}function i(t){return t instanceof a?t:a(t)}return a.prototype={_date:null,init:function(...t){var e;return t.length?(Array.isArray(t[0])?t=r.map(((e,n)=>t[0][n]||e)):null==(e=t[0])||"object"!=typeof e&&"function"!=typeof e||(t=r.map(((e,r)=>t[0][n[r]]||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])):this._date=new Date,this},getTime(){return this._date.getTime()},getUnix(){return~~(this._date.getTime()/1e3)},clone(){return a(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 n.map((e=>t[e]))},toString(){return this._date.toString()},toISOString(){return this._date.toISOString()},set(t,e){let 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},get(t){return this.toObject()[t]},change(t,e){let n=this.toObject();return this.set(t,n[t]+e)},format(t="YYYY-MM-DD HH:mm:ss"){let n=this._date,r=this.toObject(),a=n.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),i={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:a[1]+a[2]+":"+a[3],ZZ:a[1]+a[2]+a[3],A:["AM","PM"][~~(r.hour/12)],a:["am","pm"][~~(r.hour/12)],X:~~(r.timestamp/1e3),x:r.timestamp,Q:""+~~(r.month/3)};return i.MMM=e.MMM[r.month-1],i.MMMM=e.MMMM[r.month-1],i.Do=e.Do[r.day-1],i.W=e.W[r.week],i.w=e.w[r.week],t.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W|w/g,(function(t){return i[t]||""}))},startOf(t){let e=this.toObject(),i=null,s=n.indexOf(t)+1,h=this.toArray(),o=r.slice(s);return h.splice(s,o.length,...o),i="timestamp"==t?this.clone():"week"==t?a(e.year,e.month,e.day-e.week,0,0,0,0):a(...h),i},endOf(t){return this.startOf(t).change(t,"week"==t?7:1).change("millsecond",-1)},diffWith(t,e){if(!(t=i(t)).isValid())return!1;let r={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1},a=this.getTime()-t.getTime(),s=0;if(e){if(r[e])s=~~(a/r[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(),r={};return n.forEach((function(n){r[n]=e.diffWith(t,n),e.set(n,t.get(n))})),r}},isBefore(t,e="timestamp"){return t=i(t),this.get(e)<t.get(e)},isAfter(t,e="timestamp"){return t=i(t),this.get(e)>t.get(e)},isSame(t,e="timestamp"){return t=i(t),this.get(e)==t.get(e)},isBetween(t,e,n="timestamp"){return t=i(t),e=i(e),this.get(n)>t.get(n)&&this.get(n)<e.get(n)},isValid(){return!isNaN(this.getTime())}},a.prototype.init.prototype=a.prototype,a}));
1
+ /*!
2
+ * datex.js v1.0.7
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-05-10
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;const i=["year","month","day","hour","minute","second","millsecond"],a=[1970,1,1,0,0,0,0],s=(t,e)=>new Date(t.toLocaleString("en-US",{timeZone:e}));function r(...t){return new r.prototype.init(...t)}function h(t){return t instanceof r?t:r(t)}return r.setLanguage=function(e,n={}){return t[e]=Object.assign(t[e]||{},n),this},r.switchLanguage=function(t){return e=t,this},r.now=Date.now,r.switchTimezone=function(t){n=t},r.getTimezone=function(){return n},r.prototype={_date:null,_langMap:{},_lang:null,_timezone:null,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][i[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])}else this._date=new Date;var e;return this._date=s(this._date,this._timezone||n),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._date=s(this._date,this._timezone||n),this},getTimezone(){return this._timezone||n},getTime(){return this._date.getTime()},getUnix(){return~~(this._date.getTime()/1e3)},clone(){return r(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 i.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 i=this._date,a=this.toObject(),s=i.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),r={YYYY:""+a.year,YY:(""+a.year).padStart(2,"0"),MM:(""+a.month).padStart(2,"0"),M:""+a.month,DD:(""+a.day).padStart(2,"0"),D:""+a.day,HH:(""+a.hour).padStart(2,"0"),H:""+a.hour,hh:(""+a.hour%12).padStart(2,"0"),h:""+a.hour%12,mm:(""+a.minute).padStart(2,"0"),m:""+a.minute,ss:(""+a.second).padStart(2,"0"),s:""+a.second,S:""+~~(a.millsecond%1e3/100),SS:""+~~(a.millsecond%1e3/10),SSS:""+a.millsecond%1e3,Z:s[1]+s[2]+":"+s[3],ZZ:s[1]+s[2]+s[3],A:["AM","PM"][~~(a.hour/12)],a:["am","pm"][~~(a.hour/12)],X:a.timestamp,x:~~(a.timestamp/1e3),Q:""+~~(a.month/3),W:a.week},h=Object.assign({},t,this._langMap)[this._lang||e];return r.MMM=h.MMM[a.month-1],r.MMMM=h.MMMM[a.month-1],r.Do=h.Do[a.day-1],r.WW=h.WW[a.week],r.WWW=h.WWW[a.week],n.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,(function(t){return r[t]||""}))},startOf(t){let e=this.toObject(),n=null,s=i.indexOf(t)+1,h=this.toArray(),o=a.slice(s);return h.splice(s,o.length,...o),n="timestamp"==t?this.clone():"week"==t?r(e.year,e.month,e.day-e.week,0,0,0,0):r(...h),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},a=this.getTime()-t.getTime(),s=0;if(e){if(n[e])s=~~(a/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 i.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())}},r.prototype.init.prototype=r.prototype,r}));
package/index.html CHANGED
@@ -155,6 +155,36 @@
155
155
  <span class="subtitle">isValid</span>
156
156
  </a>
157
157
  </li>
158
+ <li>
159
+ <a href="#hash21">
160
+ <span class="title">设置语言包</span>
161
+ <span class="subtitle">setLanguage(lang,data)</span>
162
+ </a>
163
+ </li>
164
+ <li>
165
+ <a href="#hash22">
166
+ <span class="title">切换语言包</span>
167
+ <span class="subtitle">switchLanguage(lang)</span>
168
+ </a>
169
+ </li>
170
+ <li>
171
+ <a href="#hash23">
172
+ <span class="title">获取时区</span>
173
+ <span class="subtitle">getTimezone()</span>
174
+ </a>
175
+ </li>
176
+ <li>
177
+ <a href="#hash24">
178
+ <span class="title">切换时区</span>
179
+ <span class="subtitle">switchTimezone(timezone)</span>
180
+ </a>
181
+ </li>
182
+ <li>
183
+ <a href="#hash25">
184
+ <span class="title">返回当前时间戳(毫秒)</span>
185
+ <span class="subtitle">now()</span>
186
+ </a>
187
+ </li>
158
188
  </ul>
159
189
  </div>
160
190
  </div>
@@ -188,6 +218,7 @@
188
218
  <br>
189
219
  <p>通过时间字符串初始化</p>
190
220
  <p>运行:datex('2018-08-08')</p>
221
+ <p>运行:datex('20180808')</p>
191
222
  <p>运行:datex('2018-04-04T16:00:00.000Z')</p>
192
223
  <br>
193
224
  <p>通过时间对象初始化</p>
@@ -196,6 +227,12 @@
196
227
  <p>通过时间数组初始化</p>
197
228
  <p>运行:datex([2018,8,8,8,8,0])</p>
198
229
  <br>
230
+ <p>通过时间戳</p>
231
+ <p>运行:datex(1218124800000)</p>
232
+ <br>
233
+ <p>通过原生Date实例对象</p>
234
+ <p>运行:datex(new Date('2018-08-08'))</p>
235
+ <br>
199
236
  <p>无参数初始化</p>
200
237
  <p>运行:datex()</p>
201
238
  </div>
@@ -429,26 +466,31 @@
429
466
  </tr>
430
467
  <tr>
431
468
  <td>W</td>
432
- <td>星期一</td>
433
- <td>星期X</td>
469
+ <td>0</td>
470
+ <td>0-6,分别表示星期日-星期六 </td>
434
471
  </tr>
435
472
  <tr>
436
- <td>w</td>
473
+ <td>WW</td>
437
474
  <td>周一</td>
438
475
  <td>周x</td>
439
476
  </tr>
477
+ <tr>
478
+ <td>WWW</td>
479
+ <td>星期日</td>
480
+ <td>星期x</td>
481
+ </tr>
440
482
  <tr>
441
483
  <td>Do</td>
442
484
  <td>1st... 31st</td>
443
485
  <td>带序数词的月份里的一天</td>
444
486
  </tr>
445
487
  <tr>
446
- <td>X</td>
488
+ <td>x</td>
447
489
  <td>1410715640</td>
448
490
  <td>Unix时间戳(秒)</td>
449
491
  </tr>
450
492
  <tr>
451
- <td>x</td>
493
+ <td>X</td>
452
494
  <td>1410715640579</td>
453
495
  <td>Unix时间戳(毫秒)</td>
454
496
  </tr>
@@ -1024,6 +1066,72 @@
1024
1066
  <p>返回:false</p>
1025
1067
  </div>
1026
1068
  </div>
1069
+ <div id="hash21" class="box">
1070
+ <div class="c-hd">
1071
+ <p class="title">设置语言包</p>
1072
+ <p class="subtitle">setLanguage(lang,data)</p>
1073
+ </div>
1074
+ <div class="c-bd">
1075
+ <p>全局设置</p>
1076
+ <p>运行:datex.setLanguage('zh-CN',{'w':['周日','周一','周二','周三','周四','周五','周六']})</p>
1077
+ <br>
1078
+ <p>实例设置</p>
1079
+ <p>运行:datex().setLanguage('zh-CN',{'w':['周日','周一','周二','周三','周四','周五','周六']})</p>
1080
+ </div>
1081
+ </div>
1082
+ <div id="hash22" class="box">
1083
+ <div class="c-hd">
1084
+ <p class="title">切换语言包</p>
1085
+ <p class="subtitle">switchLanguage(lang)</p>
1086
+ </div>
1087
+ <div class="c-bd">
1088
+ <p>全局设置</p>
1089
+ <p>运行:datex.switchLanguage('en-US')</p>
1090
+ <br>
1091
+ <p>实例设置</p>
1092
+ <p>运行:datex().switchLanguage('en-US')</p>
1093
+ </div>
1094
+ </div>
1095
+ <div id="hash23" class="box">
1096
+ <div class="c-hd">
1097
+ <p class="title">获取时区</p>
1098
+ <p class="subtitle">getTimezone()</p>
1099
+ </div>
1100
+ <div class="c-bd">
1101
+ <p>全局获取</p>
1102
+ <p>运行:datex.getTimezone()</p>
1103
+ <p>返回:'Asia/Shanghai'</p>
1104
+ <br>
1105
+ <p>实例获取</p>
1106
+ <p>运行:datex().getTimezone()</p>
1107
+ <p>返回:'Asia/Shanghai'</p>
1108
+ </div>
1109
+ </div>
1110
+ <div id="hash24" class="box">
1111
+ <div class="c-hd">
1112
+ <p class="title">切换时区</p>
1113
+ <p class="subtitle">switchTimezone(timezone)</p>
1114
+ </div>
1115
+ <div class="c-bd">
1116
+ <p class="text-red">* 时区切换只改变时间字符串显示,即format方法及初始化过程,时间戳不会相应改变</p>
1117
+ <br>
1118
+ <p>全局设置</p>
1119
+ <p>运行:datex.switchTimezone('Asia/Tokyo')</p>
1120
+ <br>
1121
+ <p>实例设置</p>
1122
+ <p>运行:datex().switchTimezone('Asia/Tokyo')</p>
1123
+ </div>
1124
+ </div>
1125
+ <div id="hash25" class="box">
1126
+ <div class="c-hd">
1127
+ <p class="title">返回当前时间戳(毫秒)</p>
1128
+ <p class="subtitle">now()</p>
1129
+ </div>
1130
+ <div class="c-bd">
1131
+ <p>全局设置</p>
1132
+ <p>运行:datex.now()</p>
1133
+ </div>
1134
+ </div>
1027
1135
  </div>
1028
1136
  </div>
1029
1137
  </div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datex.js",
3
- "version": "1.0.5",
3
+ "version": "1.0.8",
4
4
  "description": "a datetime format library",
5
5
  "author": "HaoLe Zheng",
6
6
  "license": "MIT",
package/src/datex.js CHANGED
@@ -1,24 +1,27 @@
1
- let langMap = {};
2
- langMap['en-US'] = {
1
+ let _langMap = {};
2
+ _langMap['en-US'] = {
3
3
  'MMM':['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sept.','Oct.','Nov.','Dec.'],
4
4
  'MMMM':['January','February','March','April','May','June','July','August','September','October','November','December'],
5
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
- 'W':['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
7
- 'w':['Sun.','Mon.','Tues.','Wed.','Thur.','Fri.','Sat.'],
6
+ 'WW':['Sun.','Mon.','Tues.','Wed.','Thur.','Fri.','Sat.'],
7
+ 'WWW':['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
8
8
  };
9
- langMap['zh-CN'] = {
9
+ _langMap['zh-CN'] = {
10
10
  'MMM':['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
11
11
  'MMMM':['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
12
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
- 'W':['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
14
- 'w':['周日','周一','周二','周三','周四','周五','周六'],
13
+ 'WW':['周日','周一','周二','周三','周四','周五','周六'],
14
+ 'WWW':['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
15
15
  };
16
- let language = langMap['en-US'];
16
+ let _lang = 'en-US';
17
17
  if(typeof self!='undefined'&&self.navigator){
18
- language = langMap[self.navigator.language];
18
+ _lang = self.navigator.language;
19
19
  }
20
+ let _timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
21
+ let _offset = 0;
20
22
  const period = ['year','month','day','hour','minute','second','millsecond'];
21
23
  const initTime = [1970,1,1,0,0,0,0];
24
+ const convertTimeZone = (date, timeZone) => {return new Date(date.toLocaleString('en-US', { timeZone }))};
22
25
 
23
26
  function datex(...argu){
24
27
  return new datex.prototype.init(...argu);
@@ -30,40 +33,89 @@ function isObject(value){
30
33
  return value != null && (typeof value == 'object' || typeof value == 'function');
31
34
  }
32
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
+
33
53
  datex.prototype = {
34
54
  _date:null,
55
+ _langMap:{},
56
+ _lang:null,
57
+ _timezone:null,
58
+ _offset:0,
35
59
  init:function(...argu){
36
- if(!argu.length){
37
- this._date = new Date();
38
- }else{
39
- if(Array.isArray(argu[0])){
40
- argu = initTime.map((value,index)=>(argu[0][index]||value));
41
- }else if(isObject(argu[0])){
42
- argu = initTime.map((value,index)=>(argu[0][period[index]]||value));
43
- }
44
- if(argu.length==1&&typeof argu[0]=='string'){
45
- 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}))?)?/);
46
- 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}))?)?/);
47
- if(matchs1&&!matchs2){
48
- argu = [1,2,3,5,6,8,10].map(function(i,index){
49
- return +(matchs1[i]||initTime[index]);
50
- });
51
- }else if(matchs2){
52
- argu = [3,1,2,5,6,8,10].map(function(i,index){
53
- return +(matchs2[i]||initTime[index]);
54
- });
60
+ if(argu.length){
61
+ if(argu[0] instanceof Date){
62
+ this._date = argu[0];
63
+ }else{
64
+ if(Array.isArray(argu[0])){
65
+ argu = initTime.map((value,index)=>(argu[0][index]||value));
66
+ }else if(isObject(argu[0])){
67
+ argu = initTime.map((value,index)=>(argu[0][period[index]]||value));
68
+ }
69
+ if(argu.length==1&&typeof argu[0]=='string'){
70
+ 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}))?)?/);
71
+ 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}))?)?/);
72
+ let matchs3 = argu[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);
73
+ if(matchs1&&!matchs2){
74
+ argu = [1,2,3,5,6,8,10].map(function(i,index){
75
+ return +(matchs1[i]||initTime[index]);
76
+ });
77
+ }else if(matchs2){
78
+ argu = [3,1,2,5,6,8,10].map(function(i,index){
79
+ return +(matchs2[i]||initTime[index]);
80
+ });
81
+ }else if(matchs3){
82
+ argu = [1,2,3,4,5,6,7].map(function(i,index){
83
+ return +(matchs3[i]||initTime[index]);
84
+ });
85
+ }
86
+ }
87
+ if(argu.length>=3){
88
+ argu[1]--;
89
+ }
90
+ this._date = new Date(...argu);
91
+ if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
92
+ this._date.setFullYear(argu[0]);
55
93
  }
56
94
  }
57
- if(argu.length>=3){
58
- argu[1]--;
59
- }
60
- this._date = new Date(...argu);
61
- if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
62
- this._date.setFullYear(argu[0]);
95
+ if(_offset){
96
+ this._date.setTime(this._date.getTime()-_offset);
63
97
  }
98
+ }else{
99
+ this._date = new Date();
64
100
  }
65
101
  return this;
66
102
  },
103
+ setLanguage(lang,data={}){
104
+ this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
105
+ return this;
106
+ },
107
+ switchLanguage(lang){
108
+ this._lang = lang;
109
+ return this;
110
+ },
111
+ switchTimezone(timezone){
112
+ this._timezone = timezone;
113
+ this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
114
+ return this;
115
+ },
116
+ getTimezone(){
117
+ return this._timezone||_timezone;
118
+ },
67
119
  getTime(){
68
120
  return this._date.getTime();
69
121
  },
@@ -143,8 +195,11 @@ datex.prototype = {
143
195
  return this.set(unit,$[unit]+value);
144
196
  },
145
197
  format(pattern = 'YYYY-MM-DD HH:mm:ss'){
146
- let _ = this._date;
147
- let $ = this.toObject();
198
+ let that = this.clone();
199
+ let offset = this._offset||_offset;
200
+ that._date.setTime(this._date.getTime()+offset);
201
+ let _ = that._date;
202
+ let $ = that.toObject();
148
203
  let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
149
204
  let map = {
150
205
  'YYYY':''+$.year,
@@ -168,16 +223,19 @@ datex.prototype = {
168
223
  'ZZ':match[1]+match[2]+match[3],
169
224
  'A':['AM','PM'][~~($.hour/12)],
170
225
  'a':['am','pm'][~~($.hour/12)],
171
- 'X':~~($.timestamp/1000),
172
- 'x':$.timestamp,
226
+ 'X':$.timestamp,
227
+ 'x':~~($.timestamp/1000),
173
228
  'Q':''+(~~($.month/3)),
229
+ 'W':$.week
174
230
  };
231
+ let langMap = Object.assign({},_langMap,this._langMap);
232
+ let language = langMap[this._lang||_lang];
175
233
  map['MMM'] = language['MMM'][$.month-1];
176
234
  map['MMMM'] = language['MMMM'][$.month-1];
177
235
  map['Do'] = language['Do'][$.day-1];
178
- map['W'] = language['W'][$.week];
179
- map['w'] = language['w'][$.week];
180
- return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W|w/g,function(key){
236
+ map['WW'] = language['WW'][$.week];
237
+ map['WWW'] = language['WWW'][$.week];
238
+ return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,function(key){
181
239
  return map[key]||'';
182
240
  });
183
241
  },
package/test/index.js CHANGED
@@ -3,7 +3,8 @@ var test = require('tape');
3
3
  var datex = require('..');
4
4
 
5
5
  test('[Test]', function (t) {
6
- t.deepEqual(datex('2008-08-08').getTime(),1218153600000);
6
+ t.deepEqual(datex('2008-08-08').getTime(),1218124800000);
7
+ t.deepEqual(datex('20080808').getTime(),1218124800000);
7
8
  t.deepEqual(datex(2022,10,1).format('YYYY年MM月DD日'),'2022年10月01日');
8
9
  t.deepEqual(datex(2022,10,1).set('year',2020).format(),'2020-10-01 00:00:00');
9
10
  t.deepEqual(datex(2022,10,1).change('year',1).format(),'2023-10-01 00:00:00');