@@ -38,20 +38,25 @@ const dayjs = (date, c, pl) => {
38
38
return new Dayjs ( cfg ) // eslint-disable-line no-use-before-define
39
39
}
40
40
41
- const wrapper = ( date , instance ) => dayjs ( date , { locale : instance . $L } )
41
+ const wrapper = ( date , instance ) => dayjs ( date , { locale : instance . $L , utc : instance . $u } )
42
42
43
43
const Utils = U // for plugin use
44
- Utils . parseLocale = parseLocale
45
- Utils . isDayjs = isDayjs
46
- Utils . wrapper = wrapper
44
+ Utils . l = parseLocale
45
+ Utils . i = isDayjs
46
+ Utils . w = wrapper
47
47
48
- const parseDate = ( date ) => {
48
+ const parseDate = ( cfg ) => {
49
+ const { date, utc } = cfg
49
50
if ( date === null ) return new Date ( NaN ) // null is invalid
50
- if ( Utils . isUndefined ( date ) ) return new Date ( ) // today
51
- if ( date instanceof Date ) return date
51
+ if ( Utils . u ( date ) ) return new Date ( ) // today
52
+ if ( date instanceof Date ) return new Date ( date )
52
53
if ( typeof date === 'string' && ! / Z $ / i. test ( date ) ) {
53
54
const d = date . match ( C . REGEX_PARSE )
54
55
if ( d ) {
56
+ if ( utc ) {
57
+ return new Date ( Date . UTC ( d [ 1 ] , d [ 2 ] - 1 , d [ 3 ]
58
+ || 1 , d [ 4 ] || 0 , d [ 5 ] || 0 , d [ 6 ] || 0 , d [ 7 ] || 0 ) )
59
+ }
55
60
return new Date ( d [ 1 ] , d [ 2 ] - 1 , d [ 3 ] || 1 , d [ 4 ] || 0 , d [ 5 ] || 0 , d [ 6 ] || 0 , d [ 7 ] || 0 )
56
61
}
57
62
}
@@ -66,7 +71,7 @@ class Dayjs {
66
71
}
67
72
68
73
parse ( cfg ) {
69
- this . $d = parseDate ( cfg . date )
74
+ this . $d = parseDate ( cfg )
70
75
this . init ( )
71
76
}
72
77
@@ -146,21 +151,23 @@ class Dayjs {
146
151
}
147
152
148
153
startOf ( units , startOf ) { // startOf -> endOf
149
- const isStartOf = ! Utils . isUndefined ( startOf ) ? startOf : true
150
- const unit = Utils . prettyUnit ( units )
154
+ const isStartOf = ! Utils . u ( startOf ) ? startOf : true
155
+ const unit = Utils . p ( units )
151
156
const instanceFactory = ( d , m ) => {
152
- const ins = Utils . wrapper ( new Date ( this . $y , m , d ) , this )
157
+ const ins = Utils . w ( this . $u ?
158
+ Date . UTC ( this . $y , m , d ) : new Date ( this . $y , m , d ) , this )
153
159
return isStartOf ? ins : ins . endOf ( C . D )
154
160
}
155
161
const instanceFactorySet = ( method , slice ) => {
156
162
const argumentStart = [ 0 , 0 , 0 , 0 ]
157
163
const argumentEnd = [ 23 , 59 , 59 , 999 ]
158
- return Utils . wrapper ( this . toDate ( ) [ method ] . apply ( // eslint-disable-line prefer-spread
164
+ return Utils . w ( this . toDate ( ) [ method ] . apply ( // eslint-disable-line prefer-spread
159
165
this . toDate ( ) ,
160
166
( isStartOf ? argumentStart : argumentEnd ) . slice ( slice )
161
167
) , this )
162
168
}
163
169
const { $W, $M, $D } = this
170
+ const utcPad = `set${ this . $u ? 'UTC' : '' } `
164
171
switch ( unit ) {
165
172
case C . Y :
166
173
return isStartOf ? instanceFactory ( 1 , 0 ) :
@@ -175,13 +182,13 @@ class Dayjs {
175
182
}
176
183
case C . D :
177
184
case C . DATE :
178
- return instanceFactorySet ( 'setHours' , 0 )
185
+ return instanceFactorySet ( ` ${ utcPad } Hours` , 0 )
179
186
case C . H :
180
- return instanceFactorySet ( 'setMinutes' , 1 )
187
+ return instanceFactorySet ( ` ${ utcPad } Minutes` , 1 )
181
188
case C . MIN :
182
- return instanceFactorySet ( 'setSeconds' , 2 )
189
+ return instanceFactorySet ( ` ${ utcPad } Seconds` , 2 )
183
190
case C . S :
184
- return instanceFactorySet ( 'setMilliseconds' , 3 )
191
+ return instanceFactorySet ( ` ${ utcPad } Milliseconds` , 3 )
185
192
default :
186
193
return this . clone ( )
187
194
}
@@ -192,16 +199,17 @@ class Dayjs {
192
199
}
193
200
194
201
$set ( units , int ) { // private set
195
- const unit = Utils . prettyUnit ( units )
202
+ const unit = Utils . p ( units )
203
+ const utcPad = `set${ this . $u ? 'UTC' : '' } `
196
204
const name = {
197
- [ C . D ] : 'setDate' ,
198
- [ C . DATE ] : 'setDate' ,
199
- [ C . M ] : 'setMonth' ,
200
- [ C . Y ] : 'setFullYear' ,
201
- [ C . H ] : 'setHours' ,
202
- [ C . MIN ] : 'setMinutes' ,
203
- [ C . S ] : 'setSeconds' ,
204
- [ C . MS ] : 'setMilliseconds'
205
+ [ C . D ] : ` ${ utcPad } Date` ,
206
+ [ C . DATE ] : ` ${ utcPad } Date` ,
207
+ [ C . M ] : ` ${ utcPad } Month` ,
208
+ [ C . Y ] : ` ${ utcPad } FullYear` ,
209
+ [ C . H ] : ` ${ utcPad } Hours` ,
210
+ [ C . MIN ] : ` ${ utcPad } Minutes` ,
211
+ [ C . S ] : ` ${ utcPad } Seconds` ,
212
+ [ C . MS ] : ` ${ utcPad } Milliseconds`
205
213
} [ unit ]
206
214
const arg = unit === C . D ? this . $D + ( int - this . $W ) : int
207
215
@@ -217,15 +225,15 @@ class Dayjs {
217
225
218
226
add ( number , units ) {
219
227
number = Number ( number ) // eslint-disable-line no-param-reassign
220
- const unit = Utils . prettyUnit ( units )
228
+ const unit = Utils . p ( units )
221
229
const instanceFactory = ( u , n ) => {
222
230
const date = this . set ( C . DATE , 1 ) . set ( u , n + number )
223
231
return date . set ( C . DATE , Math . min ( this . $D , date . daysInMonth ( ) ) )
224
232
}
225
233
const instanceFactorySet = ( n ) => {
226
234
const date = new Date ( this . $d )
227
235
date . setDate ( date . getDate ( ) + ( n * number ) )
228
- return Utils . wrapper ( date , this )
236
+ return Utils . w ( date , this )
229
237
}
230
238
if ( unit === C . M ) {
231
239
return instanceFactory ( C . M , this . $M )
@@ -246,7 +254,7 @@ class Dayjs {
246
254
} [ unit ] || 1 // ms
247
255
248
256
const nextTimeStamp = this . valueOf ( ) + ( number * step )
249
- return Utils . wrapper ( nextTimeStamp , this )
257
+ return Utils . w ( nextTimeStamp , this )
250
258
}
251
259
252
260
subtract ( number , string ) {
@@ -257,7 +265,7 @@ class Dayjs {
257
265
if ( ! this . isValid ( ) ) return C . INVALID_DATE_STRING
258
266
259
267
const str = formatStr || C . FORMAT_DEFAULT
260
- const zoneStr = Utils . padZoneStr ( this . $d . getTimezoneOffset ( ) )
268
+ const zoneStr = Utils . z ( this )
261
269
const locale = this . $locale ( )
262
270
const {
263
271
weekdays, months
@@ -266,34 +274,34 @@ class Dayjs {
266
274
( arr && arr [ index ] ) || full [ index ] . substr ( 0 , length )
267
275
)
268
276
const get$H = num => (
269
- Utils . padStart ( this . $H % 12 || 12 , num , '0' )
277
+ Utils . s ( this . $H % 12 || 12 , num , '0' )
270
278
)
271
279
272
280
const matches = {
273
281
YY : String ( this . $y ) . slice ( - 2 ) ,
274
282
YYYY : String ( this . $y ) ,
275
283
M : String ( this . $M + 1 ) ,
276
- MM : Utils . padStart ( this . $M + 1 , 2 , '0' ) ,
284
+ MM : Utils . s ( this . $M + 1 , 2 , '0' ) ,
277
285
MMM : getShort ( locale . monthsShort , this . $M , months , 3 ) ,
278
286
MMMM : months [ this . $M ] ,
279
287
D : String ( this . $D ) ,
280
- DD : Utils . padStart ( this . $D , 2 , '0' ) ,
288
+ DD : Utils . s ( this . $D , 2 , '0' ) ,
281
289
d : String ( this . $W ) ,
282
290
dd : getShort ( locale . weekdaysMin , this . $W , weekdays , 2 ) ,
283
291
ddd : getShort ( locale . weekdaysShort , this . $W , weekdays , 3 ) ,
284
292
dddd : weekdays [ this . $W ] ,
285
293
H : String ( this . $H ) ,
286
- HH : Utils . padStart ( this . $H , 2 , '0' ) ,
294
+ HH : Utils . s ( this . $H , 2 , '0' ) ,
287
295
h : get$H ( 1 ) ,
288
296
hh : get$H ( 2 ) ,
289
297
a : this . $H < 12 ? 'am' : 'pm' ,
290
298
A : this . $H < 12 ? 'AM' : 'PM' ,
291
299
m : String ( this . $m ) ,
292
- mm : Utils . padStart ( this . $m , 2 , '0' ) ,
300
+ mm : Utils . s ( this . $m , 2 , '0' ) ,
293
301
s : String ( this . $s ) ,
294
- ss : Utils . padStart ( this . $s , 2 , '0' ) ,
295
- SSS : Utils . padStart ( this . $ms , 3 , '0' ) ,
296
- Z : zoneStr
302
+ ss : Utils . s ( this . $s , 2 , '0' ) ,
303
+ SSS : Utils . s ( this . $ms , 3 , '0' ) ,
304
+ Z : zoneStr // 'ZZ' logic below
297
305
}
298
306
299
307
return str . replace ( C . REGEX_FORMAT , ( match ) => {
@@ -309,11 +317,11 @@ class Dayjs {
309
317
}
310
318
311
319
diff ( input , units , float ) {
312
- const unit = Utils . prettyUnit ( units )
320
+ const unit = Utils . p ( units )
313
321
const that = dayjs ( input )
314
322
const zoneDelta = ( that . utcOffset ( ) - this . utcOffset ( ) ) * C . MILLISECONDS_A_MINUTE
315
323
const diff = this - that
316
- let result = Utils . monthDiff ( this , that )
324
+ let result = Utils . m ( this , that )
317
325
318
326
result = {
319
327
[ C . Y ] : result / 12 ,
@@ -326,7 +334,7 @@ class Dayjs {
326
334
[ C . S ] : diff / C . MILLISECONDS_A_SECOND
327
335
} [ unit ] || diff // milliseconds
328
336
329
- return float ? result : Utils . absFloor ( result )
337
+ return float ? result : Utils . a ( result )
330
338
}
331
339
332
340
daysInMonth ( ) {
@@ -344,7 +352,7 @@ class Dayjs {
344
352
}
345
353
346
354
clone ( ) {
347
- return Utils . wrapper ( this . toDate ( ) , this )
355
+ return Utils . w ( this . toDate ( ) , this )
348
356
}
349
357
350
358
toDate ( ) {
0 commit comments