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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 PASSER-BY
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ ## getTime() 返回时间戳(毫秒)
2
+ ```js
3
+ datex().getTime()
4
+ // => 1670768193313
5
+ ```
6
+
7
+ ## getUnix() 返回时间戳(秒)
8
+ ```js
9
+ datex().getUnix()
10
+ // => 1670768193
11
+ ```
12
+
13
+ ## clone() 返回克隆对象
14
+ ```js
15
+ datex().clone()
16
+ ```
17
+
18
+ ## toDate() 返回原生Date对象
19
+ ```js
20
+ datex().toDate()
21
+ // => Sun Dec 11 2022 22:20:20 GMT+0800 (中国标准时间)
22
+ ```
23
+
24
+ ## toObject() 返回时间字段对象
25
+ ```js
26
+ datex().toDate()
27
+ // => {
28
+ year:2022,
29
+ month:12,
30
+ day:11,
31
+ hour:22,
32
+ minute:23,
33
+ second: 14,
34
+ millsecond:612,
35
+ timestamp:1670768594612,
36
+ week:0
37
+ }
38
+ ```
39
+
40
+ ## toArray() 返回时间字段数值
41
+ ```js
42
+ datex().toArray()
43
+ // => [2022,12,11,22,23,14,612]
44
+ ```
45
+
46
+ ## toString() 返回字符串
47
+ ```js
48
+ datex().toDate()
49
+ // => Sun Dec 11 2022 22:20:20 GMT+0800 (中国标准时间)
50
+ ```
51
+
52
+ ## toISOString() 返回ISO字符串
53
+ ```js
54
+ datex().toDate()
55
+ // => 2023-04-12T07:20:23.363Z
56
+ ```
57
+
58
+ ## format(pattern) 返回格式化时间
59
+ ```js
60
+ datex(2022,10,1).format('YYYY-MM-DD HH:mm:ss')
61
+ // => 2022-10-01 00:00:00
62
+
63
+ datex(1671761818503).format('YYYY/MM/DD')
64
+ // => 2022/12/23
65
+ ```
66
+
67
+ ## set(name,value) 设置某字段值
68
+ ```js
69
+ datex(2022,10,1).set('year',2020).format()
70
+ // => 2020-10-01 00:00:00
71
+ ```
72
+
73
+ ## change(name,value) 增减某字段值
74
+ ```js
75
+ datex(2022,10,1).change('year',1).format()
76
+ // => 2022-10-01 00:00:00
77
+ ```
78
+
79
+ ## startOf(name) 获取某字段起始时
80
+ ```js
81
+ datex(2022,10,10).startOf('month').format()
82
+ // => 2022-10-01 00:00:00
83
+ ```
84
+
85
+ ## endOf(name) 获取某字段末尾时
86
+ ```js
87
+ datex(2022,10,10).endOf('month').format()
88
+ // => 2022-10-31 23:59:59
89
+ ```
90
+
91
+ ## get(name) 返回某字段值
92
+ ```js
93
+ datex(2022,10,1).get('year')
94
+ // => 2022
95
+ ```
96
+
97
+ ## diffWith(dateStr|datex,name) 返回某字段差值
98
+ ```js
99
+ datex('1949-10-01').diffWith('2022-12-01','month')
100
+ // => -878
101
+ ```
102
+
103
+ ## isBefore(dateStr|datex,name) 是否在某个时间点之前
104
+ ```js
105
+ datex('2008-08-08').isBefore('2022-02-02')
106
+ // => true
107
+ ```
108
+
109
+ ## isAfter(dateStr|datex,name) 是否在某个时间点之后
110
+ ```js
111
+ datex('2008-08-08').isAfter('2022-02-02')
112
+ // => false
113
+ ```
114
+
115
+ ## isSame(dateStr|datex,name) 是否和某个时间点相等
116
+ ```js
117
+ datex('2008-08-08').isSame('2022-02-02')
118
+ // => false
119
+ ```
120
+
121
+ ## isBetween(dateStr|datex,dateStr|datex,name) 是否在两个时间点之间
122
+ ```js
123
+ datex('2008-08-08').isBetween('2003-07-13','2022-02-02')
124
+ // => true
125
+ ```
@@ -0,0 +1,13 @@
1
+ /*!
2
+ * datex v1.0.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-04-12
12
+ */
13
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).datex=t()}(this,(function(){"use strict";let e={"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:["周日","周一","周二","周三","周四","周五","周六"]}},t=e["en-US"];function n(){return new n.prototype.init(...arguments)}function a(e){return e instanceof n||(e=n(e)),e}return"undefined"!=typeof self&&self.navigator&&(t=e[self.navigator.language]),n.prototype={_date:null,init:function(){return arguments.length>=3&&arguments[1]--,this._date=new Date(...arguments),this},getTime(){return this._date.getTime()},getUnix(){return~~(this._date.getTime()/1e3)},clone(){return n(this.getTime())},toDate(){return this._date},toObject(){let e=this._date;return{year:e.getFullYear(),month:e.getMonth()+1,day:e.getDate(),hour:e.getHours(),minute:e.getMinutes(),second:e.getSeconds(),millsecond:e.getMilliseconds(),timestamp:e.getTime(),week:e.getDay()}},toArray(){let e=this.toObject();return[e.year,e.month,e.day,e.hour,e.minute,e.second,e.millsecond]},toString(){return this._date.toString()},toISOString(){return this._date.toISOString()},set(e,t){let n=this._date,a=this.toObject();switch(e){case"year":n.setFullYear(t);break;case"month":n.setMonth(t-1);break;case"day":n.setDate(t);break;case"hour":n.setHours(t);break;case"minute":n.setMinutes(t);break;case"second":n.setSeconds(t);break;case"millsecond":n.setMilliseconds(t);break;case"timestamp":n.setTime(t);break;case"week":let e=a.week-t;n.setDate(a.day-e)}return this},get(e){return this.toObject()[e]},change(e,t){let n=this.toObject();return this.set(e,n[e]+t)},format(e="YYYY-MM-DD HH:mm:ss"){let n=this._date,a=this.toObject(),r=n.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),s={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:r[1]+r[2]+":"+r[3],ZZ:r[1]+r[2]+r[3],A:["AM","PM"][~~(a.hour/12)],a:["am","pm"][~~(a.hour/12)],X:~~(a.timestamp/1e3),x:a.timestamp,Q:""+~~(a.month/3)};return s.MMM=t.MMM[a.month-1],s.MMMM=t.MMMM[a.month-1],s.Do=t.Do[a.day-1],s.W=t.W[a.week],s.w=t.w[a.week],e.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W|w/g,(function(e){return s[e]||""}))},startOf(e){let t=this.toObject(),a=null;switch(e){case"year":a=n(t.year,1,1,0,0,0,0);break;case"month":a=n(t.year,t.month,1,0,0,0,0);break;case"day":a=n(t.year,t.month,t.day,0,0,0,0);break;case"hour":a=n(t.year,t.month,t.day,t.hour,0,0,0);break;case"minute":a=n(t.year,t.month,t.day,t.hour,t.minute,0,0);break;case"second":a=n(t.year,t.month,t.day,t.hour,t.minute,t.second,0);break;case"millsecond":case"timestamp":a=this.clone();break;case"week":a=n(t.year,t.month,t.day-t.week,0,0,0,0)}return a},endOf(e){return this.toObject(),this.startOf(e).change(e,"week"==e?7:1).change("millsecond",-1)},diffWith(e,t){if(e=a(e),isNaN(e.getTime()))return!1;let n={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1,timestamp:1},r=this.getTime()-e.getTime(),s=0;if(t){if(n[t])s=~~(r/n[t]);else if("month"==t){s=12*(this.get("year")-1)+this.get("month")-(12*(e.get("year")-1)+e.get("month")),s<0&&this.get("day")>e.get("day")?s+=1:s>0&&this.get("day")<e.get("day")&&(s-=1)}else"year"==t&&(s=this.get("year")-e.get("year"),s<0&&(this.get("month")>e.get("month")||this.get("month")==e.get("month")&&this.get("day")>e.get("day"))?s+=1:s>0&&(this.get("month")<e.get("month")||this.get("month")==e.get("month")&&this.get("day")<e.get("day"))&&(s-=1));return s}{let t=this.clone(),n={};return["year","month","day","hour","minute","second","millsecond"].forEach((function(a){n[a]=t.diffWith(e,a),t.set(a,e.get(a))})),n}},isBefore(e,t="millsecond"){return e=a(e),this.get(t)<e.get(t)},isAfter(e,t="millsecond"){return e=a(e),this.get(t)>e.get(t)},isSame(e,t="millsecond"){return e=a(e),this.get(t)==e.get(t)},isBetween(e,t,n="millsecond"){return e=a(e),t=a(t),this.get(n)>e.get(n)&&this.get(n)<t.get(n)}},n.prototype.init.prototype=n.prototype,n}));
package/gulpfile.js ADDED
@@ -0,0 +1,38 @@
1
+ const gulp = require('gulp');
2
+ const rename = require("gulp-rename");
3
+ const rollup = require('rollup');
4
+ const terser = require('@rollup/plugin-terser');
5
+ const pkg = require('./package');
6
+
7
+ // copyright
8
+ var repository = pkg.repository.url.replace(/(.+)(:\/\/.+)\.git$/,'https$2');
9
+ var now = new Date();
10
+ var date = (new Date(now.getTime()-now.getTimezoneOffset()*60000)).toISOString().substr(0,10);
11
+ var banner = `/*!
12
+ * ${pkg.name} v${pkg.version}
13
+ * ${pkg.description}
14
+ * ${pkg.homepage}
15
+ *
16
+ * Copyright (c) 2022-present, ${pkg.author}
17
+ *
18
+ * Released under the ${pkg.license} License
19
+ * ${repository}
20
+ *
21
+ * Created on: ${date}
22
+ */`;
23
+
24
+ gulp.task('default', () => {
25
+ return rollup.rollup({
26
+ input: './src/datex.js',
27
+ plugins: [
28
+ terser()
29
+ ]
30
+ }).then(bundle => {
31
+ return bundle.write({
32
+ file: './dist/datex.min.js',
33
+ format: 'umd',
34
+ name: 'datex',
35
+ banner
36
+ });
37
+ });
38
+ });