forked from iamkun/dayjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (54 loc) · 1.95 KB
/
Copy pathindex.js
File metadata and controls
66 lines (54 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { D, W, Y } from '../../constant'
const isoWeekPrettyUnit = 'isoweek'
export default (o, c, d) => {
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
}
return yearFirstDay.add(addDiffDays, D)
}
const getCurrentWeekThursday = ins => ins.add((4 - ins.isoWeekday()), D)
const proto = c.prototype
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()
}
proto.isoWeek = function (week) {
if (!this.$utils().u(week)) {
return this.add((week - this.isoWeek()) * 7, D)
}
const nowWeekThursday = getCurrentWeekThursday(this)
const diffWeekThursday = getYearFirstThursday(this.isoWeekYear(), this.$u)
return nowWeekThursday.diff(diffWeekThursday, W) + 1
}
proto.isoWeekday = function (week) {
if (!this.$utils().u(week)) {
return this.day(this.day() % 7 ? week : week - 7)
}
return this.day() || 7
}
const oldStartOf = proto.startOf
proto.startOf = function (units, startOf) {
const utils = this.$utils()
const isStartOf = !utils.u(startOf) ? startOf : true
const unit = utils.p(units)
if (unit === isoWeekPrettyUnit) {
return isStartOf ? this.date(this.date() - (this.isoWeekday() - 1)).startOf('day') :
this.date((this.date() - 1 - (this.isoWeekday() - 1)) + 7).endOf('day')
}
return oldStartOf.bind(this)(units, startOf)
}
}