@@ -218,7 +218,12 @@ class Dayjs {
218218 } [ unit ]
219219 const arg = unit === C . D ? this . $D + ( int - this . $W ) : int
220220
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 )
222227
223228 this . init ( )
224229 return this
@@ -231,21 +236,16 @@ class Dayjs {
231236 add ( number , units ) {
232237 number = Number ( number ) // eslint-disable-line no-param-reassign
233238 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- }
239239 const instanceFactorySet = ( n ) => {
240240 const date = new Date ( this . $d )
241241 date . setDate ( date . getDate ( ) + ( n * number ) )
242242 return Utils . w ( date , this )
243243 }
244244 if ( unit === C . M ) {
245- return instanceFactory ( C . M , this . $M )
245+ return this . set ( C . M , this . $M + number )
246246 }
247247 if ( unit === C . Y ) {
248- return instanceFactory ( C . Y , this . $y )
248+ return this . set ( C . Y , this . $y + number )
249249 }
250250 if ( unit === C . D ) {
251251 return instanceFactorySet ( 1 )
0 commit comments