daily-report-calendar.js 1.29 KB
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
}