File tree Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { TimeUnit } from '../types' ;
2
+ import { daysInMonth } from '../units/month' ;
2
3
3
4
const defaultTimeUnit : TimeUnit = {
4
5
year : 0 ,
@@ -30,11 +31,17 @@ export function createDate(
30
31
31
32
export function shiftDate ( date : Date , unit : TimeUnit ) : Date {
32
33
const _unit = Object . assign ( { } , defaultTimeUnit , unit ) ;
34
+ const year = date . getFullYear ( ) + _unit . year ;
35
+ const month = date . getMonth ( ) + _unit . month ;
36
+ let day = date . getDate ( ) + _unit . day ;
37
+ if ( _unit . month && ! _unit . day ) {
38
+ day = Math . min ( day , daysInMonth ( year , month ) ) ;
39
+ }
33
40
34
41
return createDate (
35
- date . getFullYear ( ) + _unit . year ,
36
- date . getMonth ( ) + _unit . month ,
37
- date . getDate ( ) + _unit . day ,
42
+ year ,
43
+ month ,
44
+ day ,
38
45
date . getHours ( ) + _unit . hour ,
39
46
date . getMinutes ( ) + _unit . minute ,
40
47
date . getSeconds ( ) + _unit . seconds
You can’t perform that action at this time.
0 commit comments