datex.js 1.0.7 → 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.
Files changed (3) hide show
  1. package/index.html +2 -0
  2. package/package.json +1 -1
  3. package/src/datex.js +46 -36
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datex.js",
3
- "version": "1.0.7",
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
@@ -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,6 +44,7 @@ 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;
@@ -53,44 +55,49 @@ datex.prototype = {
53
55
  _langMap:{},
54
56
  _lang:null,
55
57
  _timezone:null,
58
+ _offset:0,
56
59
  init:function(...argu){
57
- if(!argu.length){
58
- this._date = new Date();
59
- }else if(argu[0] instanceof Date){
60
- this._date = argu[0];
61
- }else{
62
- if(Array.isArray(argu[0])){
63
- argu = initTime.map((value,index)=>(argu[0][index]||value));
64
- }else if(isObject(argu[0])){
65
- argu = initTime.map((value,index)=>(argu[0][period[index]]||value));
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
- });
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]);
83
93
  }
84
94
  }
85
- if(argu.length>=3){
86
- argu[1]--;
87
- }
88
- this._date = new Date(...argu);
89
- if(argu.length>=2&&!isNaN(argu[0])&&argu[0]<100){
90
- this._date.setFullYear(argu[0]);
95
+ if(_offset){
96
+ this._date.setTime(this._date.getTime()-_offset);
91
97
  }
98
+ }else{
99
+ this._date = new Date();
92
100
  }
93
- this._date = convertTimeZone(this._date,this._timezone||_timezone);
94
101
  return this;
95
102
  },
96
103
  setLanguage(lang,data={}){
@@ -103,7 +110,7 @@ datex.prototype = {
103
110
  },
104
111
  switchTimezone(timezone){
105
112
  this._timezone = timezone;
106
- this._date = convertTimeZone(this._date,this._timezone||_timezone);
113
+ this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
107
114
  return this;
108
115
  },
109
116
  getTimezone(){
@@ -188,8 +195,11 @@ datex.prototype = {
188
195
  return this.set(unit,$[unit]+value);
189
196
  },
190
197
  format(pattern = 'YYYY-MM-DD HH:mm:ss'){
191
- let _ = this._date;
192
- 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();
193
203
  let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
194
204
  let map = {
195
205
  'YYYY':''+$.year,