File tree 4 files changed +23
-0
lines changed
4 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -195,6 +195,9 @@ class Dayjs {
195
195
$set ( units , int ) { // private set
196
196
const unit = Utils . prettyUnit ( units )
197
197
switch ( unit ) {
198
+ case C . D :
199
+ Utils . setDay ( this . $d , int )
200
+ break
198
201
case C . DATE :
199
202
this . $d . setDate ( int )
200
203
break
Original file line number Diff line number Diff line change @@ -40,7 +40,17 @@ const prettyUnit = (u) => {
40
40
41
41
const isUndefined = s => s === undefined
42
42
43
+ const setDay = ( date , day ) => {
44
+ const currentDay = date . getDay ( )
45
+ const distance = day - currentDay
46
+
47
+ date . setDate ( date . getDate ( ) + distance )
48
+
49
+ return date
50
+ }
51
+
43
52
export default {
53
+ setDay,
44
54
padStart,
45
55
padZoneStr,
46
56
monthDiff,
Original file line number Diff line number Diff line change @@ -46,6 +46,10 @@ it('Set Day', () => {
46
46
expect ( dayjs ( ) . set ( 'date' , 30 ) . valueOf ( ) ) . toBe ( moment ( ) . set ( 'date' , 30 ) . valueOf ( ) )
47
47
} )
48
48
49
+ it ( 'Set Day of Week' , ( ) => {
50
+ expect ( dayjs ( ) . set ( 'day' , 0 ) . valueOf ( ) ) . toBe ( moment ( ) . set ( 'day' , 0 ) . valueOf ( ) )
51
+ } )
52
+
49
53
it ( 'Set Month' , ( ) => {
50
54
expect ( dayjs ( ) . set ( 'month' , 11 ) . valueOf ( ) ) . toBe ( moment ( ) . set ( 'month' , 11 ) . valueOf ( ) )
51
55
} )
Original file line number Diff line number Diff line change @@ -26,3 +26,9 @@ it('PadStart', () => {
26
26
expect ( padStart ( 1 , 2 , '0' ) ) . toBe ( '01' )
27
27
expect ( padStart ( 0 , 2 , '0' ) ) . toBe ( '00' )
28
28
} )
29
+
30
+ it ( 'SetDate' , ( ) => {
31
+ const day = Math . floor ( ( Math . random ( ) * 7 ) % 6 )
32
+
33
+ expect ( Utils . setDay ( ( new Date ( ) ) , day ) . getDay ( ) ) . toBe ( day )
34
+ } )
You can’t perform that action at this time.
0 commit comments