datex.js 1.0.0 → 1.0.2
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/README.md +8 -0
- package/dist/datex.min.js +3 -3
- package/index.html +45 -8
- package/package.json +1 -1
- package/src/datex.js +46 -51
- package/test/index.js +9 -9
package/README.md
CHANGED
package/dist/datex.min.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* datex v1.0.
|
|
2
|
+
* datex.js v1.0.2
|
|
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-04-
|
|
11
|
+
* Created on: 2023-04-13
|
|
12
12
|
*/
|
|
13
|
-
!function(e
|
|
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)):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,o=this.toArray(),h=r.slice(s);return o.splice(s,h.length,...h),i="timestamp"==t?this.clone():"week"==t?a(e.year,e.month,e.day-e.week,0,0,0,0):a(...o),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}));
|
package/index.html
CHANGED
|
@@ -29,6 +29,12 @@
|
|
|
29
29
|
<div class="mod-menu">
|
|
30
30
|
<div class="bd">
|
|
31
31
|
<ul>
|
|
32
|
+
<li>
|
|
33
|
+
<a href="#hash0">
|
|
34
|
+
<span class="title">初始化</span>
|
|
35
|
+
<span class="subtitle">datex</span>
|
|
36
|
+
</a>
|
|
37
|
+
</li>
|
|
32
38
|
<li>
|
|
33
39
|
<a href="#hash1">
|
|
34
40
|
<span class="title">返回时间戳(毫秒)</span>
|
|
@@ -143,6 +149,12 @@
|
|
|
143
149
|
<span class="subtitle">isBetween</span>
|
|
144
150
|
</a>
|
|
145
151
|
</li>
|
|
152
|
+
<li>
|
|
153
|
+
<a href="#hash20">
|
|
154
|
+
<span class="title">是否为有效对象</span>
|
|
155
|
+
<span class="subtitle">isValid</span>
|
|
156
|
+
</a>
|
|
157
|
+
</li>
|
|
146
158
|
</ul>
|
|
147
159
|
</div>
|
|
148
160
|
</div>
|
|
@@ -165,6 +177,29 @@
|
|
|
165
177
|
<p><a class="btn" href="https://passer-by.com/datex/dist/datex.min.js" target="_blank">获取脚本</a></p>
|
|
166
178
|
</div>
|
|
167
179
|
<div class="bd">
|
|
180
|
+
<div id="hash0" class="box">
|
|
181
|
+
<div class="c-hd">
|
|
182
|
+
<p class="title">初始化</p>
|
|
183
|
+
<p class="subtitle">datex(dateStr|Array|Object|)</p>
|
|
184
|
+
</div>
|
|
185
|
+
<div class="c-bd">
|
|
186
|
+
<p>通过多个参数初始化</p>
|
|
187
|
+
<p>运行:datex(2018,8,8)</p>
|
|
188
|
+
<br>
|
|
189
|
+
<p>通过时间字符串初始化</p>
|
|
190
|
+
<p>运行:datex('2018-08-08')</p>
|
|
191
|
+
<p>运行:datex('2018-04-04T16:00:00.000Z')</p>
|
|
192
|
+
<br>
|
|
193
|
+
<p>通过时间对象初始化</p>
|
|
194
|
+
<p>运行:datex({year:2008,month:8,day:8,hour:8,minute:0,second:0})</p>
|
|
195
|
+
<br>
|
|
196
|
+
<p>通过时间数组初始化</p>
|
|
197
|
+
<p>运行:datex([2018,8,8,8,8,0])</p>
|
|
198
|
+
<br>
|
|
199
|
+
<p>无参数初始化</p>
|
|
200
|
+
<p>运行:datex()</p>
|
|
201
|
+
</div>
|
|
202
|
+
</div>
|
|
168
203
|
<div id="hash1" class="box">
|
|
169
204
|
<div class="c-hd">
|
|
170
205
|
<p class="title">返回时间戳(毫秒)</p>
|
|
@@ -738,14 +773,6 @@
|
|
|
738
773
|
<td>millsecond</td>
|
|
739
774
|
<td>毫秒</td>
|
|
740
775
|
</tr>
|
|
741
|
-
<tr>
|
|
742
|
-
<td>timestamp</td>
|
|
743
|
-
<td>时间戳</td>
|
|
744
|
-
</tr>
|
|
745
|
-
<tr>
|
|
746
|
-
<td>week</td>
|
|
747
|
-
<td>周几</td>
|
|
748
|
-
</tr>
|
|
749
776
|
</tbody>
|
|
750
777
|
</table>
|
|
751
778
|
</div>
|
|
@@ -977,6 +1004,16 @@
|
|
|
977
1004
|
</table>
|
|
978
1005
|
</div>
|
|
979
1006
|
</div>
|
|
1007
|
+
<div id="hash20" class="box">
|
|
1008
|
+
<div class="c-hd">
|
|
1009
|
+
<p class="title">是否为有效对象</p>
|
|
1010
|
+
<p class="subtitle">isValid()</p>
|
|
1011
|
+
</div>
|
|
1012
|
+
<div class="c-bd">
|
|
1013
|
+
<p>运行:datex('2008-13-12').isValid()</p>
|
|
1014
|
+
<p>返回:false</p>
|
|
1015
|
+
</div>
|
|
1016
|
+
</div>
|
|
980
1017
|
</div>
|
|
981
1018
|
</div>
|
|
982
1019
|
</div>
|
package/package.json
CHANGED
package/src/datex.js
CHANGED
|
@@ -17,24 +17,35 @@ let language = langMap['en-US'];
|
|
|
17
17
|
if(typeof self!='undefined'&&self.navigator){
|
|
18
18
|
language = langMap[self.navigator.language];
|
|
19
19
|
}
|
|
20
|
+
const period = ['year','month','day','hour','minute','second','millsecond'];
|
|
21
|
+
const initTime = [1970,1,1,0,0,0,0];
|
|
20
22
|
|
|
21
|
-
function datex(){
|
|
22
|
-
return new datex.prototype.init(...
|
|
23
|
+
function datex(...argu){
|
|
24
|
+
return new datex.prototype.init(...argu);
|
|
23
25
|
}
|
|
24
26
|
function getInstance(that){
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return
|
|
27
|
+
return that instanceof datex?that:datex(that);
|
|
28
|
+
}
|
|
29
|
+
function isObject(value){
|
|
30
|
+
return value != null && (typeof value == 'object' || typeof value == 'function');
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
datex.prototype = {
|
|
32
34
|
_date:null,
|
|
33
|
-
init:function(){
|
|
34
|
-
if(
|
|
35
|
-
|
|
35
|
+
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>=3){
|
|
45
|
+
argu[1]--;
|
|
46
|
+
}
|
|
47
|
+
this._date = new Date(...argu);
|
|
36
48
|
}
|
|
37
|
-
this._date = new Date(...arguments);
|
|
38
49
|
return this;
|
|
39
50
|
},
|
|
40
51
|
getTime(){
|
|
@@ -65,7 +76,7 @@ datex.prototype = {
|
|
|
65
76
|
},
|
|
66
77
|
toArray(){
|
|
67
78
|
let $ = this.toObject();
|
|
68
|
-
return [
|
|
79
|
+
return period.map(name=>$[name]);
|
|
69
80
|
},
|
|
70
81
|
toString(){
|
|
71
82
|
return this._date.toString();
|
|
@@ -102,8 +113,7 @@ datex.prototype = {
|
|
|
102
113
|
_.setTime(value);
|
|
103
114
|
break;
|
|
104
115
|
case 'week':
|
|
105
|
-
|
|
106
|
-
_.setDate($.day-diff);
|
|
116
|
+
_.setDate($.day-$.week+value);
|
|
107
117
|
break;
|
|
108
118
|
}
|
|
109
119
|
return this;
|
|
@@ -158,51 +168,33 @@ datex.prototype = {
|
|
|
158
168
|
startOf(unit){
|
|
159
169
|
let $ = this.toObject();
|
|
160
170
|
let that = null;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
case 'hour':
|
|
172
|
-
that = datex($.year,$.month,$.day,$.hour,0,0,0);
|
|
173
|
-
break;
|
|
174
|
-
case 'minute':
|
|
175
|
-
that = datex($.year,$.month,$.day,$.hour,$.minute,0,0);
|
|
176
|
-
break;
|
|
177
|
-
case 'second':
|
|
178
|
-
that = datex($.year,$.month,$.day,$.hour,$.minute,$.second,0);
|
|
179
|
-
break;
|
|
180
|
-
case 'millsecond':
|
|
181
|
-
case 'timestamp':
|
|
182
|
-
that = this.clone();
|
|
183
|
-
break;
|
|
184
|
-
case 'week':
|
|
185
|
-
that = datex($.year,$.month,$.day-$.week,0,0,0,0);
|
|
186
|
-
break;
|
|
171
|
+
let index = period.indexOf(unit)+1;
|
|
172
|
+
let dateSet = this.toArray();
|
|
173
|
+
let initSet = initTime.slice(index);
|
|
174
|
+
dateSet.splice(index,initSet.length,...initSet);
|
|
175
|
+
if(unit=='timestamp'){
|
|
176
|
+
that = this.clone();
|
|
177
|
+
}else if(unit=='week'){
|
|
178
|
+
that = datex($.year,$.month,$.day-$.week,0,0,0,0);
|
|
179
|
+
}else{
|
|
180
|
+
that = datex(...dateSet);
|
|
187
181
|
}
|
|
188
182
|
return that;
|
|
189
183
|
},
|
|
190
184
|
endOf(unit){
|
|
191
|
-
let $ = this.toObject();
|
|
192
185
|
return this.startOf(unit).change(unit,unit=='week'?7:1).change('millsecond',-1);
|
|
193
186
|
},
|
|
194
187
|
diffWith(that,unit){
|
|
195
188
|
that = getInstance(that);
|
|
196
|
-
if(
|
|
189
|
+
if(!that.isValid()){
|
|
197
190
|
return false;
|
|
198
191
|
}
|
|
199
192
|
let diffMap = {
|
|
200
|
-
'day':
|
|
201
|
-
'hour':
|
|
202
|
-
'minute':
|
|
193
|
+
'day':8.64e7,
|
|
194
|
+
'hour':3.6e6,
|
|
195
|
+
'minute':6e4,
|
|
203
196
|
'second':1000,
|
|
204
|
-
'millsecond':1
|
|
205
|
-
'timestamp':1
|
|
197
|
+
'millsecond':1
|
|
206
198
|
};
|
|
207
199
|
let timestamp = this.getTime()-that.getTime();
|
|
208
200
|
let value = 0;
|
|
@@ -230,29 +222,32 @@ datex.prototype = {
|
|
|
230
222
|
}else{
|
|
231
223
|
let clone = this.clone();
|
|
232
224
|
let hash = {};
|
|
233
|
-
|
|
225
|
+
period.forEach(function(unit){
|
|
234
226
|
hash[unit] = clone.diffWith(that,unit);
|
|
235
227
|
clone.set(unit,that.get(unit));
|
|
236
228
|
});
|
|
237
229
|
return hash;
|
|
238
230
|
}
|
|
239
231
|
},
|
|
240
|
-
isBefore(that,unit = '
|
|
232
|
+
isBefore(that,unit = 'timestamp'){
|
|
241
233
|
that = getInstance(that);
|
|
242
234
|
return this.get(unit)<that.get(unit);
|
|
243
235
|
},
|
|
244
|
-
isAfter(that,unit = '
|
|
236
|
+
isAfter(that,unit = 'timestamp'){
|
|
245
237
|
that = getInstance(that);
|
|
246
238
|
return this.get(unit)>that.get(unit);
|
|
247
239
|
},
|
|
248
|
-
isSame(that,unit = '
|
|
240
|
+
isSame(that,unit = 'timestamp'){
|
|
249
241
|
that = getInstance(that);
|
|
250
242
|
return this.get(unit)==that.get(unit);
|
|
251
243
|
},
|
|
252
|
-
isBetween(startDate,endDate,unit = '
|
|
244
|
+
isBetween(startDate,endDate,unit = 'timestamp'){
|
|
253
245
|
startDate = getInstance(startDate);
|
|
254
246
|
endDate = getInstance(endDate);
|
|
255
247
|
return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
|
|
248
|
+
},
|
|
249
|
+
isValid(){
|
|
250
|
+
return !isNaN(this.getTime());
|
|
256
251
|
}
|
|
257
252
|
};
|
|
258
253
|
datex.prototype.init.prototype = datex.prototype;
|
package/test/index.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
var test = require('tape');
|
|
3
|
-
var
|
|
3
|
+
var datex = require('..');
|
|
4
4
|
|
|
5
5
|
test('[Test]', function (t) {
|
|
6
|
-
t.deepEqual(
|
|
7
|
-
t.deepEqual(
|
|
8
|
-
t.deepEqual(
|
|
9
|
-
t.deepEqual(
|
|
10
|
-
t.deepEqual(
|
|
11
|
-
t.deepEqual(
|
|
12
|
-
t.deepEqual(
|
|
13
|
-
t.deepEqual(
|
|
6
|
+
t.deepEqual(datex('2008-08-08').getTime(),1218153600000);
|
|
7
|
+
t.deepEqual(datex(2022,10,1).format('YYYY年MM月DD日'),'2022年10月01日');
|
|
8
|
+
t.deepEqual(datex(2022,10,1).set('year',2020).format(),'2020-10-01 00:00:00');
|
|
9
|
+
t.deepEqual(datex(2022,10,1).change('year',1).format(),'2023-10-01 00:00:00');
|
|
10
|
+
t.deepEqual(datex(2022,10,10).startOf('month').format(),'2022-10-01 00:00:00');
|
|
11
|
+
t.deepEqual(datex(2022,10,10).endOf('month').format(),'2022-10-31 23:59:59');
|
|
12
|
+
t.deepEqual(datex(2008,8,8,23,45,45).get('hour'),23);
|
|
13
|
+
t.deepEqual(datex('1949-10-01').diffWith('2022-12-01','month'),-878);
|
|
14
14
|
t.end();
|
|
15
15
|
});
|