Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ class Dayjs {
const matches = (match) => {
switch (match) {
case 'YY':
return String(this.$y).slice(-2)
return String(this.$y)
.slice(-2)
.padStart(2, '0')
case 'YYYY':
return Utils.s(this.$y, 4, '0')
case 'M':
Expand Down
17 changes: 16 additions & 1 deletion src/plugin/isoWeek/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ export default (o, c, d) => {

const proto = c.prototype

proto.isoWeekYear = function () {
proto.isoWeekYear = function (year) {
if (!this.$utils().u(year)) {
const currentIsoWeek = this.isoWeek()
const currentIsoWeekday = this.isoWeekday()

const newYearThursday = getYearFirstThursday(year, this.$u)

const newDate = newYearThursday
.add(currentIsoWeek - 1, W)
.isoWeekday(currentIsoWeekday)

this.$d = newDate.toDate()
this.init()

return this
}
const nowWeekThursday = getCurrentWeekThursday(this)
return nowWeekThursday.year()
}
Expand Down
5 changes: 5 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ it('Format Year YY YYYY', () => {
expect(dayjs().format('YYY')).toBe(`${moment().format('YY')}Y`)
})

it('Format single digit Year YY YYYY', () => {
const date = '0001-01-01T01:01:01.000Z'
expect(dayjs(date).format('YY')).toBe(moment(date).format('YY'))
})

it('Format Month M MM MMM MMMM', () => {
expect(dayjs().format('M')).toBe(moment().format('M'))
expect(dayjs().format('MM')).toBe(moment().format('MM'))
Expand Down
42 changes: 42 additions & 0 deletions test/plugin/isoWeek.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,48 @@ it('get isoWeekYear', () => {
expect(dayjs().isoWeekYear()).toBe(moment().isoWeekYear())
})

it('2024-12-29 to ISO Year 2025, ISO Week 1, then +1 week', () => {
const testCase = {
initialDate: '2024-12-29',
targetIsoYear: 2025,
targetIsoWeek: 1,
addWeeks: 1
}

const momentResult = moment(testCase.initialDate)
.isoWeekYear(testCase.targetIsoYear)
.isoWeek(testCase.targetIsoWeek)
.add(testCase.addWeeks, 'week')

const dayjsResult = dayjs(testCase.initialDate)
.isoWeekYear(testCase.targetIsoYear)
.isoWeek(testCase.targetIsoWeek)
.add(testCase.addWeeks, 'week')

expect(dayjsResult.valueOf()).toBe(momentResult.valueOf())
})

it('2024-01-01 to ISO Year 2023, ISO Week 2, then -1 week', () => {
const testCase = {
initialDate: '2024-01-01',
targetIsoYear: 2023,
targetIsoWeek: 52,
weeks: 1
}

const momentResult = moment(testCase.initialDate)
.isoWeekYear(testCase.targetIsoYear)
.isoWeek(testCase.targetIsoWeek)
.subtract(testCase.weeks, 'week')

const dayjsResult = dayjs(testCase.initialDate)
.isoWeekYear(testCase.targetIsoYear)
.isoWeek(testCase.targetIsoWeek)
.subtract(testCase.weeks, 'week')

expect(dayjsResult.valueOf()).toBe(momentResult.valueOf())
})

it('startOf/endOf isoWeek', () => {
const ISOWEEK = 'isoWeek'
expect(dayjs().startOf(ISOWEEK).valueOf()).toBe(moment().startOf(ISOWEEK).valueOf())
Expand Down
Loading