Files
digital-pilates/utils/date.ts
richarjiang 9796c614ed feat: 添加日历功能和进度条组件
- 在项目中引入 dayjs 库以处理日期
- 新增 PlanCard 和 ProgressBar 组件,分别用于展示训练计划和进度条
- 更新首页以显示推荐的训练计划
- 优化个人中心页面的底部留白处理
- 本地化界面文本为中文
2025-08-12 09:16:59 +08:00

60 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import dayjs, { Dayjs } from 'dayjs';
import 'dayjs/locale/zh-cn';
dayjs.locale('zh-cn');
/**
* 返回基于当前时间的中文问候语:早上好 / 下午好 / 晚上好
* - 早上05:00 - 11:59
* - 下午12:00 - 17:59
* - 晚上18:00 - 04:59
*/
export function getChineseGreeting(now: Date = new Date()): string {
const hour = now.getHours();
if (hour >= 5 && hour < 12) {
return '早上好';
}
if (hour >= 12 && hour < 18) {
return '下午好';
}
return '晚上好';
}
/** 获取中文月份标题例如2025年8月 */
export function getMonthTitleZh(date: Dayjs = dayjs()): string {
return date.format('YYYY年M月');
}
export type MonthDay = {
/** 中文星期:日/一/二/三/四/五/六 */
weekdayZh: string;
/** 月内第几日1-31 */
dayOfMonth: number;
/** 对应的 dayjs 对象 */
date: Dayjs;
};
/** 获取某月的所有日期(中文星期+日号) */
export function getMonthDaysZh(date: Dayjs = dayjs()): MonthDay[] {
const year = date.year();
const monthIndex = date.month();
const daysInMonth = date.daysInMonth();
const zhWeek = ['日', '一', '二', '三', '四', '五', '六'];
return Array.from({ length: daysInMonth }, (_, i) => {
const d = dayjs(new Date(year, monthIndex, i + 1));
return {
weekdayZh: zhWeek[d.day()],
dayOfMonth: i + 1,
date: d,
};
});
}
/** 获取“今天”在当月的索引0 基) */
export function getTodayIndexInMonth(date: Dayjs = dayjs()): number {
return date.date() - 1;
}