Skip to content

Commit

Permalink
fix: Fix isoWeek Plugin cal bug in UTC mode (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Aug 8, 2020
1 parent b07182b commit f2e5f32
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/plugin/isoWeek/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { D, W, Y } from '../../constant'
const isoWeekPrettyUnit = 'isoweek'

export default (o, c, d) => {
const getYearFirstThursday = (year) => {
const yearFirstDay = d().year(year).startOf(Y)
const getYearFirstThursday = (year, isUtc) => {
const yearFirstDay = (isUtc ? d.utc : d)().year(year).startOf(Y)
let addDiffDays = 4 - yearFirstDay.isoWeekday()
if (yearFirstDay.isoWeekday() > 4) {
addDiffDays += 7
Expand All @@ -26,7 +26,7 @@ export default (o, c, d) => {
return this.add((week - this.isoWeek()) * 7, D)
}
const nowWeekThursday = getCurrentWeekThursday(this)
const diffWeekThursday = getYearFirstThursday(this.isoWeekYear())
const diffWeekThursday = getYearFirstThursday(this.isoWeekYear(), this.$u)
return nowWeekThursday.diff(diffWeekThursday, W) + 1
}

Expand Down
10 changes: 10 additions & 0 deletions test/plugin/isoWeek.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import isoWeek from '../../src/plugin/isoWeek'
import utc from '../../src/plugin/utc'

dayjs.extend(isoWeek)
dayjs.extend(utc)

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -124,3 +126,11 @@ it('isoWeek of year', () => {
expect(dayjs('20210110').isoWeekYear()).toBe(2021)
expect(dayjs('20210110').isoWeek()).toBe(1)
})


it('utc mode', () => {
// Wednesday, 1 January 2020 00:00:00 UTC
const d = dayjs.utc(1577836800000).isoWeek()
expect(d).toBe(1)
expect(moment.utc(1577836800000).isoWeek()).toBe(d)
})

0 comments on commit f2e5f32

Please sign in to comment.