@@ -218,7 +218,12 @@ class Dayjs {
218
218
} [ unit ]
219
219
const arg = unit === C . D ? this . $D + ( int - this . $W ) : int
220
220
221
- if ( this . $d [ name ] ) this . $d [ name ] ( arg )
221
+ if ( unit === C . M || unit === C . Y ) {
222
+ const date = this . clone ( ) . set ( C . DATE , 1 )
223
+ date . $d [ name ] ( arg )
224
+ date . init ( )
225
+ this . $d = date . set ( C . DATE , Math . min ( this . $D , date . daysInMonth ( ) ) ) . toDate ( )
226
+ } else if ( name ) this . $d [ name ] ( arg )
222
227
223
228
this . init ( )
224
229
return this
@@ -231,21 +236,16 @@ class Dayjs {
231
236
add ( number , units ) {
232
237
number = Number ( number ) // eslint-disable-line no-param-reassign
233
238
const unit = Utils . p ( units )
234
- const instanceFactory = ( u , n ) => {
235
- // clone is for badMutable plugin
236
- const date = this . clone ( ) . set ( C . DATE , 1 ) . set ( u , n + number )
237
- return date . set ( C . DATE , Math . min ( this . $D , date . daysInMonth ( ) ) )
238
- }
239
239
const instanceFactorySet = ( n ) => {
240
240
const date = new Date ( this . $d )
241
241
date . setDate ( date . getDate ( ) + ( n * number ) )
242
242
return Utils . w ( date , this )
243
243
}
244
244
if ( unit === C . M ) {
245
- return instanceFactory ( C . M , this . $M )
245
+ return this . set ( C . M , this . $M + number )
246
246
}
247
247
if ( unit === C . Y ) {
248
- return instanceFactory ( C . Y , this . $y )
248
+ return this . set ( C . Y , this . $y + number )
249
249
}
250
250
if ( unit === C . D ) {
251
251
return instanceFactorySet ( 1 )
0 commit comments