Skip to content

win11Calendar

Win11 风格日历辅助函数:月视图格子、农历/节气副标题、头部日期文案。

函数签名

typescript
function isSameDay(left: Date, right: Date): boolean
function isSameMonth(left: Date, right: Date): boolean
function addMonths(date: Date, months: number): Date
function getSolarTerm(date: Date): string | null
function formatCalendarSubLabel(date: Date): string
function formatLunarFullLabel(date: Date): string
function formatFlyoutDateHeading(date: Date): string
function formatMonthYearLabel(date: Date): string
function buildMonthCells(viewMonth: Date, today: Date): CalendarCell[]

interface CalendarCell {
  date: Date
  day: number
  subLabel: string
  inCurrentMonth: boolean
  isToday: boolean
}

const WEEKDAY_LABELS: readonly string[]

参数

函数参数说明
isSameDay / isSameMonthleft, right比较两个 Date 是否同一天/同一月
addMonthsdate, months偏移月份,结果归一化到该月 1 日
getSolarTermdate读取近似节气名,无匹配返回 null
formatCalendarSubLabeldate格子副标题:节气优先,否则农历短标签
formatLunarFullLabeldate完整农历行,如「六月初九」
formatFlyoutDateHeadingdate头部公历行,如「7月22日,星期三」
formatMonthYearLabeldate月标题,如「2026年7月」
buildMonthCellsviewMonth, today生成 6×7 月视图格子数组

返回值

函数类型说明
isSameDay / isSameMonthboolean是否匹配
addMonthsDate偏移后的日期
getSolarTermstring | null节气名或 null
format*string本地化文案
buildMonthCellsCalendarCell[]42 个格子,周一为一周起始

工作原理

  1. 月视图buildMonthCellsviewMonth 所在月为中心,补齐前后空白至 6 行;inCurrentMonth 标记是否属于当前月,isToday 对比 today 的零点日期。
  2. 农历:通过 Intl.DateTimeFormat('zh-CN-u-ca-chinese') 读取农历月日;初一显示月份名,其余日用 LUNAR_DAY_NAMES 映射。
  3. 节气SOLAR_TERMS 表按公历月-日近似匹配,命中时优先于农历显示在 formatCalendarSubLabel
  4. 头部文案formatFlyoutDateHeading 使用 zh-CN 格式化并在「日」与「星期」之间插入逗号,贴近 Win11 中文日历。