datex.js 1.0.4 → 1.0.7
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 +13 -13
- package/index.html +111 -5
- package/package.json +1 -1
- package/src/datex.js +75 -14
- package/test/index.js +2 -1
package/dist/datex.min.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* datex.js v1.0.
|
|
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-
|
|
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"],
|
|
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
|
|
433
|
-
<td
|
|
469
|
+
<td>0</td>
|
|
470
|
+
<td>0-6,分别表示星期日-星期六 </td>
|
|
434
471
|
</tr>
|
|
435
472
|
<tr>
|
|
436
|
-
<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>
|
|
488
|
+
<td>x</td>
|
|
447
489
|
<td>1410715640</td>
|
|
448
490
|
<td>Unix时间戳(秒)</td>
|
|
449
491
|
</tr>
|
|
450
492
|
<tr>
|
|
451
|
-
<td>
|
|
493
|
+
<td>X</td>
|
|
452
494
|
<td>1410715640579</td>
|
|
453
495
|
<td>Unix时间戳(毫秒)</td>
|
|
454
496
|
</tr>
|
|
@@ -1024,6 +1066,70 @@
|
|
|
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>全局设置</p>
|
|
1117
|
+
<p>运行:datex.switchTimezone('Asia/Tokyo')</p>
|
|
1118
|
+
<br>
|
|
1119
|
+
<p>实例设置</p>
|
|
1120
|
+
<p>运行:datex().switchTimezone('Asia/Tokyo')</p>
|
|
1121
|
+
</div>
|
|
1122
|
+
</div>
|
|
1123
|
+
<div id="hash25" class="box">
|
|
1124
|
+
<div class="c-hd">
|
|
1125
|
+
<p class="title">返回当前时间戳(毫秒)</p>
|
|
1126
|
+
<p class="subtitle">now()</p>
|
|
1127
|
+
</div>
|
|
1128
|
+
<div class="c-bd">
|
|
1129
|
+
<p>全局设置</p>
|
|
1130
|
+
<p>运行:datex.now()</p>
|
|
1131
|
+
</div>
|
|
1132
|
+
</div>
|
|
1027
1133
|
</div>
|
|
1028
1134
|
</div>
|
|
1029
1135
|
</div>
|
package/package.json
CHANGED
package/src/datex.js
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
let
|
|
2
|
-
|
|
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
|
-
'
|
|
7
|
-
'
|
|
6
|
+
'WW':['Sun.','Mon.','Tues.','Wed.','Thur.','Fri.','Sat.'],
|
|
7
|
+
'WWW':['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
|
|
8
8
|
};
|
|
9
|
-
|
|
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
|
-
'
|
|
14
|
-
'
|
|
13
|
+
'WW':['周日','周一','周二','周三','周四','周五','周六'],
|
|
14
|
+
'WWW':['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
|
|
15
15
|
};
|
|
16
|
-
let
|
|
16
|
+
let _lang = 'en-US';
|
|
17
17
|
if(typeof self!='undefined'&&self.navigator){
|
|
18
|
-
|
|
18
|
+
_lang = self.navigator.language;
|
|
19
19
|
}
|
|
20
|
+
let _timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
20
21
|
const period = ['year','month','day','hour','minute','second','millsecond'];
|
|
21
22
|
const initTime = [1970,1,1,0,0,0,0];
|
|
23
|
+
const convertTimeZone = (date, timeZone) => {return new Date(date.toLocaleString('en-US', { timeZone }))};
|
|
22
24
|
|
|
23
25
|
function datex(...argu){
|
|
24
26
|
return new datex.prototype.init(...argu);
|
|
@@ -30,17 +32,56 @@ function isObject(value){
|
|
|
30
32
|
return value != null && (typeof value == 'object' || typeof value == 'function');
|
|
31
33
|
}
|
|
32
34
|
|
|
35
|
+
datex.setLanguage = function(lang,data={}){
|
|
36
|
+
_langMap[lang] = Object.assign(_langMap[lang]||{},data);
|
|
37
|
+
return this;
|
|
38
|
+
};
|
|
39
|
+
datex.switchLanguage = function(lang){
|
|
40
|
+
_lang = lang;
|
|
41
|
+
return this;
|
|
42
|
+
};
|
|
43
|
+
datex.now = Date.now;
|
|
44
|
+
datex.switchTimezone = function(timezone){
|
|
45
|
+
_timezone = timezone;
|
|
46
|
+
};
|
|
47
|
+
datex.getTimezone = function(){
|
|
48
|
+
return _timezone;
|
|
49
|
+
};
|
|
50
|
+
|
|
33
51
|
datex.prototype = {
|
|
34
52
|
_date:null,
|
|
53
|
+
_langMap:{},
|
|
54
|
+
_lang:null,
|
|
55
|
+
_timezone:null,
|
|
35
56
|
init:function(...argu){
|
|
36
57
|
if(!argu.length){
|
|
37
58
|
this._date = new Date();
|
|
59
|
+
}else if(argu[0] instanceof Date){
|
|
60
|
+
this._date = argu[0];
|
|
38
61
|
}else{
|
|
39
62
|
if(Array.isArray(argu[0])){
|
|
40
63
|
argu = initTime.map((value,index)=>(argu[0][index]||value));
|
|
41
64
|
}else if(isObject(argu[0])){
|
|
42
65
|
argu = initTime.map((value,index)=>(argu[0][period[index]]||value));
|
|
43
66
|
}
|
|
67
|
+
if(argu.length==1&&typeof argu[0]=='string'){
|
|
68
|
+
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}))?)?/);
|
|
69
|
+
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}))?)?/);
|
|
70
|
+
let matchs3 = argu[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);
|
|
71
|
+
if(matchs1&&!matchs2){
|
|
72
|
+
argu = [1,2,3,5,6,8,10].map(function(i,index){
|
|
73
|
+
return +(matchs1[i]||initTime[index]);
|
|
74
|
+
});
|
|
75
|
+
}else if(matchs2){
|
|
76
|
+
argu = [3,1,2,5,6,8,10].map(function(i,index){
|
|
77
|
+
return +(matchs2[i]||initTime[index]);
|
|
78
|
+
});
|
|
79
|
+
}else if(matchs3){
|
|
80
|
+
argu = [1,2,3,4,5,6,7].map(function(i,index){
|
|
81
|
+
return +(matchs3[i]||initTime[index]);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
44
85
|
if(argu.length>=3){
|
|
45
86
|
argu[1]--;
|
|
46
87
|
}
|
|
@@ -49,8 +90,25 @@ datex.prototype = {
|
|
|
49
90
|
this._date.setFullYear(argu[0]);
|
|
50
91
|
}
|
|
51
92
|
}
|
|
93
|
+
this._date = convertTimeZone(this._date,this._timezone||_timezone);
|
|
52
94
|
return this;
|
|
53
95
|
},
|
|
96
|
+
setLanguage(lang,data={}){
|
|
97
|
+
this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
|
|
98
|
+
return this;
|
|
99
|
+
},
|
|
100
|
+
switchLanguage(lang){
|
|
101
|
+
this._lang = lang;
|
|
102
|
+
return this;
|
|
103
|
+
},
|
|
104
|
+
switchTimezone(timezone){
|
|
105
|
+
this._timezone = timezone;
|
|
106
|
+
this._date = convertTimeZone(this._date,this._timezone||_timezone);
|
|
107
|
+
return this;
|
|
108
|
+
},
|
|
109
|
+
getTimezone(){
|
|
110
|
+
return this._timezone||_timezone;
|
|
111
|
+
},
|
|
54
112
|
getTime(){
|
|
55
113
|
return this._date.getTime();
|
|
56
114
|
},
|
|
@@ -155,16 +213,19 @@ datex.prototype = {
|
|
|
155
213
|
'ZZ':match[1]+match[2]+match[3],
|
|
156
214
|
'A':['AM','PM'][~~($.hour/12)],
|
|
157
215
|
'a':['am','pm'][~~($.hour/12)],
|
|
158
|
-
'X'
|
|
159
|
-
'x'
|
|
216
|
+
'X':$.timestamp,
|
|
217
|
+
'x':~~($.timestamp/1000),
|
|
160
218
|
'Q':''+(~~($.month/3)),
|
|
219
|
+
'W':$.week
|
|
161
220
|
};
|
|
221
|
+
let langMap = Object.assign({},_langMap,this._langMap);
|
|
222
|
+
let language = langMap[this._lang||_lang];
|
|
162
223
|
map['MMM'] = language['MMM'][$.month-1];
|
|
163
224
|
map['MMMM'] = language['MMMM'][$.month-1];
|
|
164
225
|
map['Do'] = language['Do'][$.day-1];
|
|
165
|
-
map['
|
|
166
|
-
map['
|
|
167
|
-
return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W
|
|
226
|
+
map['WW'] = language['WW'][$.week];
|
|
227
|
+
map['WWW'] = language['WWW'][$.week];
|
|
228
|
+
return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,function(key){
|
|
168
229
|
return map[key]||'';
|
|
169
230
|
});
|
|
170
231
|
},
|
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(),
|
|
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');
|