datex.js 1.0.0
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/LICENSE +21 -0
- package/README.md +125 -0
- package/dist/datex.min.js +13 -0
- package/gulpfile.js +38 -0
- package/index.html +1037 -0
- package/package.json +35 -0
- package/src/datex.js +260 -0
- package/static/image/bg.svg +54 -0
- package/static/style/index.css +459 -0
- package/test/index.js +15 -0
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "datex.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "a datetime format library",
|
|
5
|
+
"author": "HaoLe Zheng",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"datetime",
|
|
9
|
+
"format",
|
|
10
|
+
"时间格式化"
|
|
11
|
+
],
|
|
12
|
+
"main": "dist/datex.min.js",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/mumuy/datex.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/mumuy/datex/issues"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@rollup/plugin-terser": "^0.4.1",
|
|
22
|
+
"gulp": "^4.0.2",
|
|
23
|
+
"gulp-rename": "^2.0.0",
|
|
24
|
+
"rollup": "^3.20.2",
|
|
25
|
+
"tape": "^5.6.3"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "node test/index.js",
|
|
29
|
+
"build": "gulp && npm run test"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://passer-by.com/datex/",
|
|
32
|
+
"directories": {
|
|
33
|
+
"test": "test"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/datex.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
let langMap = {};
|
|
2
|
+
langMap['en-US'] = {
|
|
3
|
+
'MMM':['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sept.','Oct.','Nov.','Dec.'],
|
|
4
|
+
'MMMM':['January','February','March','April','May','June','July','August','September','October','November','December'],
|
|
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.'],
|
|
8
|
+
};
|
|
9
|
+
langMap['zh-CN'] = {
|
|
10
|
+
'MMM':['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
|
|
11
|
+
'MMMM':['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
|
|
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':['周日','周一','周二','周三','周四','周五','周六'],
|
|
15
|
+
};
|
|
16
|
+
let language = langMap['en-US'];
|
|
17
|
+
if(typeof self!='undefined'&&self.navigator){
|
|
18
|
+
language = langMap[self.navigator.language];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function datex(){
|
|
22
|
+
return new datex.prototype.init(...arguments);
|
|
23
|
+
}
|
|
24
|
+
function getInstance(that){
|
|
25
|
+
if(!(that instanceof datex)){
|
|
26
|
+
that = datex(that);
|
|
27
|
+
}
|
|
28
|
+
return that;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
datex.prototype = {
|
|
32
|
+
_date:null,
|
|
33
|
+
init:function(){
|
|
34
|
+
if(arguments.length>=3){
|
|
35
|
+
arguments[1]--;
|
|
36
|
+
}
|
|
37
|
+
this._date = new Date(...arguments);
|
|
38
|
+
return this;
|
|
39
|
+
},
|
|
40
|
+
getTime(){
|
|
41
|
+
return this._date.getTime();
|
|
42
|
+
},
|
|
43
|
+
getUnix(){
|
|
44
|
+
return ~~(this._date.getTime()/1000);
|
|
45
|
+
},
|
|
46
|
+
clone(){
|
|
47
|
+
return datex(this.getTime());
|
|
48
|
+
},
|
|
49
|
+
toDate(){
|
|
50
|
+
return this._date;
|
|
51
|
+
},
|
|
52
|
+
toObject(){
|
|
53
|
+
let _ = this._date;
|
|
54
|
+
return {
|
|
55
|
+
'year':_.getFullYear(),
|
|
56
|
+
'month':_.getMonth()+1,
|
|
57
|
+
'day':_.getDate(),
|
|
58
|
+
'hour':_.getHours(),
|
|
59
|
+
'minute':_.getMinutes(),
|
|
60
|
+
'second':_.getSeconds(),
|
|
61
|
+
'millsecond':_.getMilliseconds(),
|
|
62
|
+
'timestamp':_.getTime(),
|
|
63
|
+
'week':_.getDay()
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
toArray(){
|
|
67
|
+
let $ = this.toObject();
|
|
68
|
+
return [$.year,$.month,$.day,$.hour,$.minute,$.second,$.millsecond];
|
|
69
|
+
},
|
|
70
|
+
toString(){
|
|
71
|
+
return this._date.toString();
|
|
72
|
+
},
|
|
73
|
+
toISOString(){
|
|
74
|
+
return this._date.toISOString();
|
|
75
|
+
},
|
|
76
|
+
set(unit,value){
|
|
77
|
+
let _ = this._date;
|
|
78
|
+
let $ = this.toObject();
|
|
79
|
+
switch (unit) {
|
|
80
|
+
case 'year':
|
|
81
|
+
_.setFullYear(value);
|
|
82
|
+
break;
|
|
83
|
+
case 'month':
|
|
84
|
+
_.setMonth(value-1);
|
|
85
|
+
break;
|
|
86
|
+
case 'day':
|
|
87
|
+
_.setDate(value);
|
|
88
|
+
break;
|
|
89
|
+
case 'hour':
|
|
90
|
+
_.setHours(value);
|
|
91
|
+
break;
|
|
92
|
+
case 'minute':
|
|
93
|
+
_.setMinutes(value);
|
|
94
|
+
break;
|
|
95
|
+
case 'second':
|
|
96
|
+
_.setSeconds(value);
|
|
97
|
+
break;
|
|
98
|
+
case 'millsecond':
|
|
99
|
+
_.setMilliseconds(value);
|
|
100
|
+
break;
|
|
101
|
+
case 'timestamp':
|
|
102
|
+
_.setTime(value);
|
|
103
|
+
break;
|
|
104
|
+
case 'week':
|
|
105
|
+
let diff = $.week-value;
|
|
106
|
+
_.setDate($.day-diff);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
return this;
|
|
110
|
+
},
|
|
111
|
+
get(unit){
|
|
112
|
+
let $ = this.toObject();
|
|
113
|
+
return $[unit];
|
|
114
|
+
},
|
|
115
|
+
change(unit,value){
|
|
116
|
+
let $ = this.toObject();
|
|
117
|
+
return this.set(unit,$[unit]+value);
|
|
118
|
+
},
|
|
119
|
+
format(pattern = 'YYYY-MM-DD HH:mm:ss'){
|
|
120
|
+
let _ = this._date;
|
|
121
|
+
let $ = this.toObject();
|
|
122
|
+
let match = _.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/);
|
|
123
|
+
let map = {
|
|
124
|
+
'YYYY':''+$.year,
|
|
125
|
+
'YY':(''+$.year).padStart(2,'0'),
|
|
126
|
+
'MM':(''+$.month).padStart(2,'0'),
|
|
127
|
+
'M':''+$.month,
|
|
128
|
+
'DD':(''+$.day).padStart(2,'0'),
|
|
129
|
+
'D':''+$.day,
|
|
130
|
+
'HH':(''+$.hour).padStart(2,'0'),
|
|
131
|
+
'H':''+$.hour,
|
|
132
|
+
'hh':(''+($.hour%12)).padStart(2,'0'),
|
|
133
|
+
'h':''+($.hour%12),
|
|
134
|
+
'mm':(''+$.minute).padStart(2,'0'),
|
|
135
|
+
'm':''+$.minute,
|
|
136
|
+
'ss':(''+$.second).padStart(2,'0'),
|
|
137
|
+
's':''+$.second,
|
|
138
|
+
'S':''+(~~(($.millsecond%1000)/100)),
|
|
139
|
+
'SS':''+(~~(($.millsecond%1000)/10)),
|
|
140
|
+
'SSS':''+($.millsecond%1000),
|
|
141
|
+
'Z':match[1]+match[2]+':'+match[3],
|
|
142
|
+
'ZZ':match[1]+match[2]+match[3],
|
|
143
|
+
'A':['AM','PM'][~~($.hour/12)],
|
|
144
|
+
'a':['am','pm'][~~($.hour/12)],
|
|
145
|
+
'X':~~($.timestamp/1000),
|
|
146
|
+
'x':$.timestamp,
|
|
147
|
+
'Q':''+(~~($.month/3)),
|
|
148
|
+
};
|
|
149
|
+
map['MMM'] = language['MMM'][$.month-1];
|
|
150
|
+
map['MMMM'] = language['MMMM'][$.month-1];
|
|
151
|
+
map['Do'] = language['Do'][$.day-1];
|
|
152
|
+
map['W'] = language['W'][$.week];
|
|
153
|
+
map['w'] = language['w'][$.week];
|
|
154
|
+
return pattern.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W|w/g,function(key){
|
|
155
|
+
return map[key]||'';
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
startOf(unit){
|
|
159
|
+
let $ = this.toObject();
|
|
160
|
+
let that = null;
|
|
161
|
+
switch (unit) {
|
|
162
|
+
case 'year':
|
|
163
|
+
that = datex($.year,1,1,0,0,0,0);
|
|
164
|
+
break;
|
|
165
|
+
case 'month':
|
|
166
|
+
that = datex($.year,$.month,1,0,0,0,0);
|
|
167
|
+
break;
|
|
168
|
+
case 'day':
|
|
169
|
+
that = datex($.year,$.month,$.day,0,0,0,0);
|
|
170
|
+
break;
|
|
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;
|
|
187
|
+
}
|
|
188
|
+
return that;
|
|
189
|
+
},
|
|
190
|
+
endOf(unit){
|
|
191
|
+
let $ = this.toObject();
|
|
192
|
+
return this.startOf(unit).change(unit,unit=='week'?7:1).change('millsecond',-1);
|
|
193
|
+
},
|
|
194
|
+
diffWith(that,unit){
|
|
195
|
+
that = getInstance(that);
|
|
196
|
+
if(isNaN(that.getTime())){
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
let diffMap = {
|
|
200
|
+
'day':86400000,
|
|
201
|
+
'hour':3600000,
|
|
202
|
+
'minute':60000,
|
|
203
|
+
'second':1000,
|
|
204
|
+
'millsecond':1,
|
|
205
|
+
'timestamp':1
|
|
206
|
+
};
|
|
207
|
+
let timestamp = this.getTime()-that.getTime();
|
|
208
|
+
let value = 0;
|
|
209
|
+
if(unit){
|
|
210
|
+
if(diffMap[unit]){
|
|
211
|
+
value = ~~(timestamp/diffMap[unit]);
|
|
212
|
+
}else if(unit=='month'){
|
|
213
|
+
let this_month = 12*(this.get('year')-1)+this.get('month');
|
|
214
|
+
let that_month = 12*(that.get('year')-1)+that.get('month');
|
|
215
|
+
value = this_month -that_month;
|
|
216
|
+
if(value<0&&this.get('day')>that.get('day')){
|
|
217
|
+
value+=1;
|
|
218
|
+
}else if(value>0&&this.get('day')<that.get('day')){
|
|
219
|
+
value-=1;
|
|
220
|
+
}
|
|
221
|
+
}else if(unit=='year'){
|
|
222
|
+
value = this.get('year') - that.get('year');
|
|
223
|
+
if(value<0&&(this.get('month')>that.get('month')||this.get('month')==that.get('month')&&this.get('day')>that.get('day'))){
|
|
224
|
+
value+=1;
|
|
225
|
+
}else if(value>0&&(this.get('month')<that.get('month')||this.get('month')==that.get('month')&&this.get('day')<that.get('day'))){
|
|
226
|
+
value-=1;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return value;
|
|
230
|
+
}else{
|
|
231
|
+
let clone = this.clone();
|
|
232
|
+
let hash = {};
|
|
233
|
+
['year','month','day','hour','minute','second','millsecond'].forEach(function(unit){
|
|
234
|
+
hash[unit] = clone.diffWith(that,unit);
|
|
235
|
+
clone.set(unit,that.get(unit));
|
|
236
|
+
});
|
|
237
|
+
return hash;
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
isBefore(that,unit = 'millsecond'){
|
|
241
|
+
that = getInstance(that);
|
|
242
|
+
return this.get(unit)<that.get(unit);
|
|
243
|
+
},
|
|
244
|
+
isAfter(that,unit = 'millsecond'){
|
|
245
|
+
that = getInstance(that);
|
|
246
|
+
return this.get(unit)>that.get(unit);
|
|
247
|
+
},
|
|
248
|
+
isSame(that,unit = 'millsecond'){
|
|
249
|
+
that = getInstance(that);
|
|
250
|
+
return this.get(unit)==that.get(unit);
|
|
251
|
+
},
|
|
252
|
+
isBetween(startDate,endDate,unit = 'millsecond'){
|
|
253
|
+
startDate = getInstance(startDate);
|
|
254
|
+
endDate = getInstance(endDate);
|
|
255
|
+
return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
datex.prototype.init.prototype = datex.prototype;
|
|
259
|
+
|
|
260
|
+
export default datex;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<svg width="1440px" height="448px" viewBox="0 0 1440 448" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
3
|
+
<!-- Generator: Sketch 59.1 (86144) - https://sketch.com -->
|
|
4
|
+
<title>编组 5备份</title>
|
|
5
|
+
<desc>Created with Sketch.</desc>
|
|
6
|
+
<defs>
|
|
7
|
+
<linearGradient x1="-5.68700053%" y1="45.5298642%" x2="84.7892757%" y2="53.934985%" id="linearGradient-1">
|
|
8
|
+
<stop stop-color="#FBFCFD" offset="0%"></stop>
|
|
9
|
+
<stop stop-color="#F8FCFF" offset="100%"></stop>
|
|
10
|
+
</linearGradient>
|
|
11
|
+
<radialGradient cx="49.4552285%" cy="50%" fx="49.4552285%" fy="50%" r="94.8348304%" gradientTransform="translate(0.494552,0.500000),scale(0.311111,1.000000),rotate(90.000000),translate(-0.494552,-0.500000)" id="radialGradient-2">
|
|
12
|
+
<stop stop-color="#FFFFFF" stop-opacity="0.5" offset="0%"></stop>
|
|
13
|
+
<stop stop-color="#EDF6FF" stop-opacity="0.578179633" offset="100%"></stop>
|
|
14
|
+
</radialGradient>
|
|
15
|
+
<rect id="path-3" x="0" y="0" width="1440" height="448"></rect>
|
|
16
|
+
<linearGradient x1="72.8463444%" y1="12.5451885%" x2="72.8463444%" y2="295.836589%" id="linearGradient-5">
|
|
17
|
+
<stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
|
|
18
|
+
<stop stop-color="#9FD7FF" stop-opacity="0.383058348" offset="100%"></stop>
|
|
19
|
+
</linearGradient>
|
|
20
|
+
<linearGradient x1="16.6159843%" y1="49.1386719%" x2="5.85340543%" y2="50.8613281%" id="linearGradient-6">
|
|
21
|
+
<stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
|
|
22
|
+
<stop stop-color="#F2F7FC" offset="100%"></stop>
|
|
23
|
+
</linearGradient>
|
|
24
|
+
<rect id="path-7" x="0" y="259" width="1440" height="189"></rect>
|
|
25
|
+
<linearGradient x1="54.7550093%" y1="16.6478641%" x2="54.7550093%" y2="112.331979%" id="linearGradient-8">
|
|
26
|
+
<stop stop-color="#FFFFFF" stop-opacity="0" offset="0%"></stop>
|
|
27
|
+
<stop stop-color="#D2ECFF" stop-opacity="0.225387893" offset="100%"></stop>
|
|
28
|
+
</linearGradient>
|
|
29
|
+
</defs>
|
|
30
|
+
<g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
31
|
+
<g id="导航方案备份">
|
|
32
|
+
<g id="编组-5备份">
|
|
33
|
+
<g id="路径备份">
|
|
34
|
+
<g id="蒙版">
|
|
35
|
+
<g transform="translate(0.000000, 0.000000)">
|
|
36
|
+
<mask id="mask-4" fill="white">
|
|
37
|
+
<use xlink:href="#path-3"></use>
|
|
38
|
+
</mask>
|
|
39
|
+
<g>
|
|
40
|
+
<use fill="url(#linearGradient-1)" xlink:href="#path-3"></use>
|
|
41
|
+
<use fill="url(#radialGradient-2)" xlink:href="#path-3"></use>
|
|
42
|
+
</g>
|
|
43
|
+
<g id="矩形备份-32" mask="url(#mask-4)">
|
|
44
|
+
<use fill="url(#linearGradient-5)" xlink:href="#path-7"></use>
|
|
45
|
+
<use fill="url(#linearGradient-6)" xlink:href="#path-7"></use>
|
|
46
|
+
</g>
|
|
47
|
+
<rect id="矩形" fill="url(#linearGradient-8)" mask="url(#mask-4)" x="0" y="166" width="1440" height="282"></rect>
|
|
48
|
+
</g>
|
|
49
|
+
</g>
|
|
50
|
+
</g>
|
|
51
|
+
</g>
|
|
52
|
+
</g>
|
|
53
|
+
</g>
|
|
54
|
+
</svg>
|