@@ -257,6 +257,15 @@ class Duration {
257
257
asYears ( ) { return this . as ( 'years' ) }
258
258
}
259
259
260
+ const manipulateDuration = ( date , duration , k ) =>
261
+ date . add ( duration . years ( ) * k , 'y' )
262
+ . add ( duration . months ( ) * k , 'M' )
263
+ . add ( duration . days ( ) * k , 'd' )
264
+ . add ( duration . hours ( ) * k , 'h' )
265
+ . add ( duration . minutes ( ) * k , 'm' )
266
+ . add ( duration . seconds ( ) * k , 's' )
267
+ . add ( duration . milliseconds ( ) * k , 'ms' )
268
+
260
269
export default ( option , Dayjs , dayjs ) => {
261
270
$d = dayjs
262
271
$u = dayjs ( ) . $utils ( )
@@ -268,12 +277,20 @@ export default (option, Dayjs, dayjs) => {
268
277
269
278
const oldAdd = Dayjs . prototype . add
270
279
const oldSubtract = Dayjs . prototype . subtract
280
+
271
281
Dayjs . prototype . add = function ( value , unit ) {
272
- if ( isDuration ( value ) ) value = value . asMilliseconds ( )
282
+ if ( isDuration ( value ) ) {
283
+ return manipulateDuration ( this , value , 1 )
284
+ }
285
+
273
286
return oldAdd . bind ( this ) ( value , unit )
274
287
}
288
+
275
289
Dayjs . prototype . subtract = function ( value , unit ) {
276
- if ( isDuration ( value ) ) value = value . asMilliseconds ( )
290
+ if ( isDuration ( value ) ) {
291
+ return manipulateDuration ( this , value , - 1 )
292
+ }
293
+
277
294
return oldSubtract . bind ( this ) ( value , unit )
278
295
}
279
296
}
0 commit comments