Skip to content

Commit f483c66

Browse files
authored
Merge pull request #270 from bas080/feat/set-day-of-week
Add setting the day of the week
2 parents d344fa3 + 9da1ac6 commit f483c66

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ class Dayjs {
195195
$set(units, int) { // private set
196196
const unit = Utils.prettyUnit(units)
197197
switch (unit) {
198+
case C.D:
199+
Utils.setDay(this.$d, int)
200+
break
198201
case C.DATE:
199202
this.$d.setDate(int)
200203
break

src/utils.js

+10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ const prettyUnit = (u) => {
4040

4141
const isUndefined = s => s === undefined
4242

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+
4352
export default {
53+
setDay,
4454
padStart,
4555
padZoneStr,
4656
monthDiff,

test/get-set.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ it('Set Day', () => {
4646
expect(dayjs().set('date', 30).valueOf()).toBe(moment().set('date', 30).valueOf())
4747
})
4848

49+
it('Set Day of Week', () => {
50+
expect(dayjs().set('day', 0).valueOf()).toBe(moment().set('day', 0).valueOf())
51+
})
52+
4953
it('Set Month', () => {
5054
expect(dayjs().set('month', 11).valueOf()).toBe(moment().set('month', 11).valueOf())
5155
})

test/utils.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ it('PadStart', () => {
2626
expect(padStart(1, 2, '0')).toBe('01')
2727
expect(padStart(0, 2, '0')).toBe('00')
2828
})
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+
})

0 commit comments

Comments
 (0)