HarmonyOSBaseLibs/src/main/ets/utils/TimeHelper.ts
徐勤民 d9a0ae8b49 feat(hospital): 添加聊天记录展示和倒计时功能
- 新增 ChatItemView 组件用于展示聊天记录
- 在 HosChatView 中集成聊天记录展示和倒计时功能
- 添加 OrderCountdownView 组件用于展示订单倒计时
- 优化时间格式化方法
2025-03-12 17:19:30 +08:00

76 行
2.4 KiB
TypeScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

/**
* 时间相关方法
* 字符 含义 举例
* y小写y 年 yyyy-2015
* M大写M 月 MM-01
* d小写d 日(某月份对应的天数) dd-02
* H24 小时制,大写H 小时0-23 HH-17
* h12 小时制,小写h 小时1-12 hh-05
* m小写m 分钟 mm-19
* s小写s 秒 ss-22
* Y大写y Week Year YYYY-2015
* D大写D 一年中的天数年份为大写Y起作用 DD-362
* S大写S 毫秒 SSS-043
*/
export class TimeHelper {
private constructor() {
}
/**
* 获取当前时间戳
* @returns
*/
static getTimeMillis() {
return new Date().getTime()
}
/**
* 获取当前时间,指定返回样式
* @param formats 时间格式
* 'yyyy-MM-dd HH:mm:ss'
*/
static getTime(format: string = 'yyyy-MM-dd HH:mm:ss') {
return TimeHelper.formatDate(new Date(), format)
}
/**
* 时间格式化
* @param date number类型为毫秒级时间戳
* @param format
* @returns
*/
static formatDate(date: Date | number, format: string = 'yyyy-MM-dd HH:mm:ss') {
if (typeof date === 'number') {
return this.formatDate(new Date(date), format);
} else {
const replacements: { [key: string]: string } = {
yyyy: date.getFullYear().toString(),
MM: String(date.getMonth() + 1).padStart(2, "0"),
dd: String(date.getDate()).padStart(2, "0"),
HH: String(date.getHours()).padStart(2, "0"),
mm: String(date.getMinutes()).padStart(2, "0"),
ss: String(date.getSeconds()).padStart(2, "0")
};
return format.replace(/yyyy|MM|dd|HH|mm|ss/g, matched => replacements[matched]);
}
}
// // 时间戳转指定类型字符串(毫秒级)
// static formatDateForTime(time: number, format: string) {
// return this.formatDate(new Date(time), format);
// }
/**
* 获取月份天数
* @param year
* @param month
* @returns
*/
static getMonthDays(year: number, month: number) {
return new Date(year, month, 0).getDate()
}
}