daily-report-calendar.js
1.29 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
var timeUtil = require('../../utils/timeFormat/time-utils.js')
function formatYmd(date) {
const d = date instanceof Date ? date : new Date(date)
const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0')
return `${y}-${m}-${day}`
}
function countSundaysBetween(startDate, endDate) {
const start = new Date(startDate)
const end = new Date(endDate)
let sundays = 0
for (let current = new Date(start); current <= end; current.setDate(current.getDate() + 1)) {
if (current.getDay() === 0) {
sundays++
}
}
return sundays
}
/**
* 根据 reportDate 计算周历 selectWeek / timeBean(对齐旧 staffDaily.initData)
* @param {string} reportDate yyyy-MM-dd
* @param {Date} [now]
*/
function resolveWeekStateForReportDate(reportDate, now = new Date()) {
const target = new Date(reportDate)
const todayStr = formatYmd(now)
let countSundays = countSundaysBetween(reportDate, todayStr)
if (now.getDay() === 0) {
countSundays -= 1
}
const selectWeek = 0 - countSundays
const timeBean = timeUtil.getWeekDayList(selectWeek, todayStr)
timeBean.selectDay = target.getDay() === 0 ? 6 : target.getDay() - 1
return { selectWeek, timeBean }
}
module.exports = {
formatYmd,
countSundaysBetween,
resolveWeekStateForReportDate
}