Files
digital-pilates/utils/date.ts
richarjiang fbe0c92f0f feat(i18n): 全面实现应用核心功能模块的国际化支持
- 新增 i18n 翻译资源,覆盖睡眠、饮水、体重、锻炼、用药 AI 识别、步数、健身圆环、基础代谢及设置等核心模块
- 重构相关页面及组件(如 SleepDetail, WaterDetail, WorkoutHistory 等)使用 `useI18n` 钩子替换硬编码文本
- 升级 `utils/date` 工具库与 `DateSelector` 组件,支持基于语言环境的日期格式化与显示
- 完善登录页、注销流程及权限申请弹窗的双语提示信息
- 优化部分页面的 UI 细节与字体样式以适配多语言显示
2025-11-27 17:54:36 +08:00

117 lines
3.1 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/en';
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('YY年M月');
}
export type MonthDay = {
/** 中文星期:日/一/二/三/四/五/六 */
weekdayZh: string;
/** 简化的星期,用于显示 */
dayAbbr: string;
/** 月内第几日1-31 */
dayOfMonth: number;
/** 对应的 dayjs 对象 */
date: Dayjs;
/** 是否是今天 */
isToday: boolean;
};
/** 获取某月的所有日期(中文星期+日号) */
export function getMonthDaysZh(date: Dayjs = dayjs()): MonthDay[] {
const year = date.year();
const monthIndex = date.month();
const daysInMonth = date.daysInMonth();
const zhWeek = ['日', '一', '二', '三', '四', '五', '六'];
const today = dayjs();
return Array.from({ length: daysInMonth }, (_, i) => {
const d = dayjs(new Date(year, monthIndex, i + 1));
const isToday = d.isSame(today, 'day');
return {
weekdayZh: zhWeek[d.day()],
dayAbbr: zhWeek[d.day()],
dayOfMonth: i + 1,
date: d,
isToday,
};
});
}
/** 获取"今天"在当月的索引0 基) */
export function getTodayIndexInMonth(date: Dayjs = dayjs()): number {
return date.date() - 1;
}
/** 获取某月的所有日期(多语言版本) */
export function getMonthDays(date: Dayjs = dayjs(), locale: 'zh' | 'en' = 'zh'): MonthDay[] {
const year = date.year();
const monthIndex = date.month();
const daysInMonth = date.daysInMonth();
// 根据语言选择星期显示
const weekDays = locale === 'zh'
? ['日', '一', '二', '三', '四', '五', '六']
: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const today = dayjs();
return Array.from({ length: daysInMonth }, (_, i) => {
const d = dayjs(new Date(year, monthIndex, i + 1));
const isToday = d.isSame(today, 'day');
return {
weekdayZh: weekDays[d.day()], // 保持原有字段名兼容性
dayAbbr: weekDays[d.day()],
dayOfMonth: i + 1,
date: d,
isToday,
};
});
}
/** 获取本地化的月份日期格式 */
export function getLocalizedDateFormat(date: Dayjs, locale: 'zh' | 'en' = 'zh'): string {
if (locale === 'zh') {
return date.format('M月D日');
} else {
return date.format('MMM D');
}
}
/** 获取本地化的月份标题 */
export function getMonthTitle(date: Dayjs = dayjs(), locale: 'zh' | 'en' = 'zh'): string {
if (locale === 'zh') {
return date.format('YY年M月');
} else {
return date.format('MMM YYYY');
}
}