datex.js 1.0.7 → 1.0.9
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 +2 -2
- package/index.html +2 -0
- package/package.json +1 -1
- package/src/datex.js +65 -51
package/dist/datex.min.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* datex.js v1.0.
|
|
2
|
+
* datex.js v1.0.9
|
|
3
3
|
* a datetime format library
|
|
4
4
|
* https://passer-by.com/datex/
|
|
5
5
|
*
|
|
@@ -10,4 +10,4 @@
|
|
|
10
10
|
*
|
|
11
11
|
* Created on: 2023-05-10
|
|
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;const
|
|
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}));
|
package/index.html
CHANGED
|
@@ -1113,6 +1113,8 @@
|
|
|
1113
1113
|
<p class="subtitle">switchTimezone(timezone)</p>
|
|
1114
1114
|
</div>
|
|
1115
1115
|
<div class="c-bd">
|
|
1116
|
+
<p class="text-red">* 时区切换只改变时间字符串显示,即format方法及初始化过程,时间戳不会相应改变</p>
|
|
1117
|
+
<br>
|
|
1116
1118
|
<p>全局设置</p>
|
|
1117
1119
|
<p>运行:datex.switchTimezone('Asia/Tokyo')</p>
|
|
1118
1120
|
<br>
|
package/package.json
CHANGED
package/src/datex.js
CHANGED
|
@@ -18,6 +18,7 @@ if(typeof self!='undefined'&&self.navigator){
|
|
|
18
18
|
_lang = self.navigator.language;
|
|
19
19
|
}
|
|
20
20
|
let _timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
21
|
+
let _offset = 0;
|
|
21
22
|
const period = ['year','month','day','hour','minute','second','millsecond'];
|
|
22
23
|
const initTime = [1970,1,1,0,0,0,0];
|
|
23
24
|
const convertTimeZone = (date, timeZone) => {return new Date(date.toLocaleString('en-US', { timeZone }))};
|
|
@@ -43,72 +44,63 @@ datex.switchLanguage = function(lang){
|
|
|
43
44
|
datex.now = Date.now;
|
|
44
45
|
datex.switchTimezone = function(timezone){
|
|
45
46
|
_timezone = timezone;
|
|
47
|
+
_offset = convertTimeZone(new Date('1970/1/1'),_timezone).getTime() - (new Date('1970/1/1')).getTime();
|
|
46
48
|
};
|
|
47
49
|
datex.getTimezone = function(){
|
|
48
50
|
return _timezone;
|
|
49
51
|
};
|
|
52
|
+
datex.supportedTimezones = Intl.supportedValuesOf('timeZone');
|
|
50
53
|
|
|
51
54
|
datex.prototype = {
|
|
52
55
|
_date:null,
|
|
53
56
|
_langMap:{},
|
|
54
57
|
_lang:null,
|
|
55
58
|
_timezone:null,
|
|
59
|
+
_offset:0,
|
|
56
60
|
init:function(...argu){
|
|
57
|
-
if(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
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]);
|
|
83
94
|
}
|
|
84
95
|
}
|
|
85
|
-
if(
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
this._date = new Date(...argu);
|
|
89
|
-
if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
|
|
90
|
-
this._date.setFullYear(argu[0]);
|
|
96
|
+
if(_offset){
|
|
97
|
+
this._date.setTime(this._date.getTime()-_offset);
|
|
91
98
|
}
|
|
99
|
+
}else{
|
|
100
|
+
this._date = new Date();
|
|
92
101
|
}
|
|
93
|
-
this._date = convertTimeZone(this._date,this._timezone||_timezone);
|
|
94
|
-
return this;
|
|
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
102
|
return this;
|
|
108
103
|
},
|
|
109
|
-
getTimezone(){
|
|
110
|
-
return this._timezone||_timezone;
|
|
111
|
-
},
|
|
112
104
|
getTime(){
|
|
113
105
|
return this._date.getTime();
|
|
114
106
|
},
|
|
@@ -188,8 +180,11 @@ datex.prototype = {
|
|
|
188
180
|
return this.set(unit,$[unit]+value);
|
|
189
181
|
},
|
|
190
182
|
format(pattern = 'YYYY-MM-DD HH:mm:ss'){
|
|
191
|
-
let
|
|
192
|
-
let
|
|
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();
|
|
193
188
|
let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
|
|
194
189
|
let map = {
|
|
195
190
|
'YYYY':''+$.year,
|
|
@@ -310,6 +305,25 @@ datex.prototype = {
|
|
|
310
305
|
endDate = getInstance(endDate);
|
|
311
306
|
return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
|
|
312
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
|
+
},
|
|
313
327
|
isValid(){
|
|
314
328
|
return !isNaN(this.getTime());
|
|
315
329
|
}
|