Skip to content

Commit aaab0c6

Browse files
authored
Merge pull request #24 from xx45/dev
Dev
2 parents c1beb5f + af83c26 commit aaab0c6

File tree

8 files changed

+210
-110
lines changed

8 files changed

+210
-110
lines changed

rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import babel from 'rollup-plugin-babel'
22
import uglify from 'rollup-plugin-uglify'
3+
import packageInfo from './package.json';
34

45
export default {
56
input: 'src/index.js',
67
output: {
7-
file: 'dist/index.js',
8+
file: `dist/${packageInfo.name}.min.js`,
89
format: 'umd',
910
name: 'dayjs'
1011
},

src/constant.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@ export const SECONDS_A_HOUR = SECONDS_A_MINUTE * 60
33
export const SECONDS_A_DAY = SECONDS_A_HOUR * 24
44
export const SECONDS_A_WEEK = SECONDS_A_DAY * 7
55

6-
export const MILLISECONDS_A_SECOND = 1000
6+
export const MILLISECONDS_A_SECOND = 1e3
77
export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND
88
export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND
99
export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND
1010
export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND
11+
12+
export const MS = 'millisecond'
13+
export const S = 'second'
14+
export const MIN = 'minute'
15+
export const H = 'hour'
16+
export const D = 'day'
17+
export const W = 'week'
18+
export const M = 'month'
19+
export const Q = 'quarter'
20+
export const Y = 'year'
21+
export const DATE = 'date'

src/index.js

Lines changed: 126 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Constant from './constant'
1+
import * as C from './constant'
22
import * as Utils from './utils'
33

44
const parseConfig = (config) => {
@@ -16,49 +16,69 @@ const parseConfig = (config) => {
1616

1717
class Dayjs {
1818
constructor(config) {
19-
this.$date = parseConfig(config)
19+
this.$d = parseConfig(config)
2020
this.init()
2121
}
2222

2323
init() {
24-
this.timeZone = this.$date.getTimezoneOffset() / 60
24+
this.timeZone = this.$d.getTimezoneOffset() / 60
2525
this.timeZoneString = Utils.padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+')
26-
this.$year = this.$date.getFullYear()
27-
this.$month = this.$date.getMonth()
28-
this.$day = this.$date.getDate()
29-
this.$week = this.$date.getDay()
30-
this.$hour = this.$date.getHours()
31-
this.$minute = this.$date.getMinutes()
32-
this.$second = this.$date.getSeconds()
33-
this.$milliseconds = this.$date.getMilliseconds()
26+
this.$y = this.$d.getFullYear()
27+
this.$M = this.$d.getMonth()
28+
this.$D = this.$d.getDate()
29+
this.$W = this.$d.getDay()
30+
this.$H = this.$d.getHours()
31+
this.$m = this.$d.getMinutes()
32+
this.$s = this.$d.getSeconds()
33+
this.$ms = this.$d.getMilliseconds()
34+
}
35+
36+
isValid() {
37+
return !(this.$d.toString() === 'Invalid Date')
38+
}
39+
40+
isLeapYear() {
41+
return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0)
42+
}
43+
44+
isSame(that) {
45+
return this.valueOf() === that.valueOf()
46+
}
47+
48+
isBefore(that) {
49+
return this.valueOf() < that.valueOf()
50+
}
51+
52+
isAfter(that) {
53+
return this.valueOf() > that.valueOf()
3454
}
3555

3656
year() {
37-
return this.$year
57+
return this.$y
3858
}
3959

4060
month() {
41-
return this.$month
61+
return this.$M
4262
}
4363

4464
date() {
45-
return this.$day
65+
return this.$D
4666
}
4767

4868
hour() {
49-
return this.$hour
69+
return this.$H
5070
}
5171

5272
minute() {
53-
return this.$minute
73+
return this.$m
5474
}
5575

5676
second() {
57-
return this.$second
77+
return this.$s
5878
}
5979

6080
millisecond() {
61-
return this.$milliseconds
81+
return this.$ms
6282
}
6383

6484
unix() {
@@ -67,22 +87,23 @@ class Dayjs {
6787

6888
valueOf() {
6989
// timezone(hour) * 60 * 60 * 1000 => ms
70-
return this.$date.getTime()
90+
return this.$d.getTime()
7191
}
7292

73-
startOf(arg, isStartOf = true) {
74-
switch (arg) {
75-
case 'year':
93+
startOf(units, isStartOf = true) { // isStartOf -> endOf
94+
const unit = Utils.prettyUnit(units)
95+
switch (unit) {
96+
case C.Y:
7697
if (isStartOf) {
77-
return new Dayjs(new Date(this.year(), 0, 1))
98+
return new Dayjs(new Date(this.$y, 0, 1))
7899
}
79-
return new Dayjs(new Date(this.year(), 11, 31)).endOf('day')
80-
case 'month':
100+
return new Dayjs(new Date(this.$y, 11, 31)).endOf('day')
101+
case C.M:
81102
if (isStartOf) {
82-
return new Dayjs(new Date(this.year(), this.month(), 1))
103+
return new Dayjs(new Date(this.$y, this.$M, 1))
83104
}
84-
return new Dayjs(new Date(this.year(), this.month() + 1, 0)).endOf('day')
85-
case 'day':
105+
return new Dayjs(new Date(this.$y, this.$M + 1, 0)).endOf('day')
106+
case C.D:
86107
if (isStartOf) {
87108
return new Dayjs(this.toDate().setHours(0, 0, 0, 0))
88109
}
@@ -96,17 +117,17 @@ class Dayjs {
96117
return this.startOf(arg, false)
97118
}
98119

99-
set(string, int) {
100-
if (!Utils.isNumber(int)) return this
101-
switch (string) {
102-
case 'date':
103-
this.$date.setDate(int)
120+
mSet(units, int) {
121+
const unit = Utils.prettyUnit(units)
122+
switch (unit) {
123+
case C.DATE:
124+
this.$d.setDate(int)
104125
break
105-
case 'month':
106-
this.$date.setMonth(int)
126+
case C.M:
127+
this.$d.setMonth(int)
107128
break
108-
case 'year':
109-
this.$date.setFullYear(int)
129+
case C.Y:
130+
this.$d.setFullYear(int)
110131
break
111132
default:
112133
break
@@ -115,34 +136,41 @@ class Dayjs {
115136
return this
116137
}
117138

118-
add(number, string) {
119-
if (['M', 'months'].indexOf(string) > -1) {
120-
const date = this.clone()
121-
date.set('date', 1)
122-
date.set('month', this.month() + number)
123-
date.set('date', Math.min(this.date(), date.daysInMonth()))
139+
set(string, int) {
140+
if (!Utils.isNumber(int)) return this
141+
return this.clone().mSet(string, int)
142+
}
143+
144+
add(number, units) {
145+
const unit = (units && units.length === 1) ? units : Utils.prettyUnit(units)
146+
if (['M', C.M].indexOf(unit) > -1) {
147+
let date = this.set(C.DATE, 1).set(C.M, this.$M + number)
148+
date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
124149
return date
125150
}
151+
if (['y', C.Y].indexOf(unit) > -1) {
152+
return this.set(C.Y, this.$y + number)
153+
}
126154
let step
127-
switch (string) {
155+
switch (unit) {
128156
case 'm':
129-
case 'minutes':
130-
step = Constant.MILLISECONDS_A_MINUTE
157+
case C.MIN:
158+
step = C.MILLISECONDS_A_MINUTE
131159
break
132160
case 'h':
133-
case 'hours':
134-
step = Constant.MILLISECONDS_A_HOUR
161+
case C.H:
162+
step = C.MILLISECONDS_A_HOUR
135163
break
136164
case 'd':
137-
case 'days':
138-
step = Constant.MILLISECONDS_A_DAY
165+
case C.D:
166+
step = C.MILLISECONDS_A_DAY
139167
break
140168
case 'w':
141-
case 'weeks':
142-
step = Constant.MILLISECONDS_A_WEEK
169+
case C.W:
170+
step = C.MILLISECONDS_A_WEEK
143171
break
144172
default: // s seconds
145-
step = Constant.MILLISECONDS_A_SECOND
173+
step = C.MILLISECONDS_A_SECOND
146174
}
147175
const nextTimeStamp = this.valueOf() + (number * step)
148176
return new Dayjs(nextTimeStamp)
@@ -158,33 +186,33 @@ class Dayjs {
158186
return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => {
159187
switch (match) {
160188
case 'YY':
161-
return String(this.$year).slice(-2)
189+
return String(this.$y).slice(-2)
162190
case 'YYYY':
163-
return String(this.$year)
191+
return String(this.$y)
164192
case 'M':
165-
return String(this.$month + 1)
193+
return String(this.$M + 1)
166194
case 'MM':
167-
return Utils.padStart(String(this.$month + 1), 2, '0')
195+
return Utils.padStart(String(this.$M + 1), 2, '0')
168196
case 'D':
169-
return String(this.$day)
197+
return String(this.$D)
170198
case 'DD':
171-
return Utils.padStart(String(this.$day), 2, '0')
199+
return Utils.padStart(String(this.$D), 2, '0')
172200
case 'd':
173-
return String(this.$week)
201+
return String(this.$W)
174202
case 'dddd':
175-
return weeks[this.$week]
203+
return weeks[this.$W]
176204
case 'H':
177-
return String(this.$hour)
205+
return String(this.$H)
178206
case 'HH':
179-
return Utils.padStart(String(this.$hour), 2, '0')
207+
return Utils.padStart(String(this.$H), 2, '0')
180208
case 'm':
181-
return String(this.$minute)
209+
return String(this.$m)
182210
case 'mm':
183-
return Utils.padStart(String(this.$minute), 2, '0')
211+
return Utils.padStart(String(this.$m), 2, '0')
184212
case 's':
185-
return String(this.$second)
213+
return String(this.$s)
186214
case 'ss':
187-
return Utils.padStart(String(this.$second), 2, '0')
215+
return Utils.padStart(String(this.$s), 2, '0')
188216
case 'Z':
189217
return `${this.timeZoneString.slice(0, -2)}:00`
190218
default: // 'ZZ'
@@ -193,27 +221,28 @@ class Dayjs {
193221
})
194222
}
195223

196-
diff(otherDate, unit, float = false) {
197-
const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate)
198-
const diff = this - other
199-
let result = Utils.monthDiff(this, other)
224+
diff(input, units, float = false) {
225+
const unit = Utils.prettyUnit(units)
226+
const that = input instanceof Dayjs ? input : new Dayjs(input)
227+
const diff = this - that
228+
let result = Utils.monthDiff(this, that)
200229
switch (unit) {
201-
case 'years':
230+
case C.Y:
202231
result /= 12
203232
break
204-
case 'months':
233+
case C.M:
205234
break
206-
case 'quarters':
235+
case C.Q:
207236
result /= 3
208237
break
209-
case 'weeks':
210-
result = diff / Constant.MILLISECONDS_A_WEEK
238+
case C.W:
239+
result = diff / C.MILLISECONDS_A_WEEK
211240
break
212-
case 'days':
213-
result = diff / Constant.MILLISECONDS_A_DAY
241+
case C.D:
242+
result = diff / C.MILLISECONDS_A_DAY
214243
break
215-
case 'seconds':
216-
result = diff / Constant.MILLISECONDS_A_SECOND
244+
case C.S:
245+
result = diff / C.MILLISECONDS_A_SECOND
217246
break
218247
default: // milliseconds
219248
result = diff
@@ -222,26 +251,26 @@ class Dayjs {
222251
}
223252

224253
daysInMonth() {
225-
return this.endOf('month').date()
254+
return this.endOf(C.M).$D
226255
}
227256

228257
clone() {
229258
return new Dayjs(this)
230259
}
231260

232261
toDate() {
233-
return new Date(this.$date)
262+
return new Date(this.$d)
234263
}
235264

236265
toArray() {
237266
return [
238-
this.year(),
239-
this.month(),
240-
this.date(),
241-
this.hour(),
242-
this.minute(),
243-
this.second(),
244-
this.millisecond()
267+
this.$y,
268+
this.$M,
269+
this.$D,
270+
this.$H,
271+
this.$m,
272+
this.$s,
273+
this.$ms
245274
]
246275
}
247276

@@ -255,18 +284,18 @@ class Dayjs {
255284

256285
toObject() {
257286
return {
258-
years: this.year(),
259-
months: this.month(),
260-
date: this.date(),
261-
hours: this.hour(),
262-
minutes: this.minute(),
263-
seconds: this.second(),
264-
milliseconds: this.millisecond()
287+
years: this.$y,
288+
months: this.$M,
289+
date: this.$D,
290+
hours: this.$H,
291+
minutes: this.$m,
292+
seconds: this.$s,
293+
milliseconds: this.$ms
265294
}
266295
}
267296

268297
toString() {
269-
return this.$date.toUTCString()
298+
return this.$d.toUTCString()
270299
}
271300
}
272301

0 commit comments

Comments
 (0)