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 / isSameMonth | left, right | 比较两个 Date 是否同一天/同一月 |
addMonths | date, months | 偏移月份,结果归一化到该月 1 日 |
getSolarTerm | date | 读取近似节气名,无匹配返回 null |
formatCalendarSubLabel | date | 格子副标题:节气优先,否则农历短标签 |
formatLunarFullLabel | date | 完整农历行,如「六月初九」 |
formatFlyoutDateHeading | date | 头部公历行,如「7月22日,星期三」 |
formatMonthYearLabel | date | 月标题,如「2026年7月」 |
buildMonthCells | viewMonth, today | 生成 6×7 月视图格子数组 |
返回值
| 函数 | 类型 | 说明 |
|---|---|---|
isSameDay / isSameMonth | boolean | 是否匹配 |
addMonths | Date | 偏移后的日期 |
getSolarTerm | string | null | 节气名或 null |
format* | string | 本地化文案 |
buildMonthCells | CalendarCell[] | 42 个格子,周一为一周起始 |
工作原理
- 月视图:
buildMonthCells以viewMonth所在月为中心,补齐前后空白至 6 行;inCurrentMonth标记是否属于当前月,isToday对比today的零点日期。 - 农历:通过
Intl.DateTimeFormat('zh-CN-u-ca-chinese')读取农历月日;初一显示月份名,其余日用LUNAR_DAY_NAMES映射。 - 节气:
SOLAR_TERMS表按公历月-日近似匹配,命中时优先于农历显示在formatCalendarSubLabel。 - 头部文案:
formatFlyoutDateHeading使用zh-CN格式化并在「日」与「星期」之间插入逗号,贴近 Win11 中文日历。