1
0
mirror of https://github.com/ialley-workshop-open/uni-halo.git synced 2026-07-27 12:30:41 +08:00
Files
uni-halo/src/utils/base/timeFrom.ts
T
小莫唐尼 862d176593 refactor: 项目整体重构与功能完善
1. 配置开发环境接口地址
2. 重构工具函数目录结构,迁移防抖节流等工具到base目录
3. 统一页面布局样式为全屏居中布局
4. 新增大量通用工具函数:深克隆、深合并、uuid、时间格式化、随机数、数组打乱、剪贴板操作等
5. 优化自定义tabbar实现,修复交互逻辑
6. 新增列表数据管理hook和请求loading封装
7. 移除无用测试文件和冗余代码
8. 完善首页内容,增加轮播图、作者卡片、金刚区等模块
2026-06-11 23:33:54 +08:00

53 lines
1.9 KiB
TypeScript

import timeFormat from './timeFormat'
/**
* 时间戳转为多久之前
* @param dateTime 时间戳、Date对象或null,默认当前时间
* @param format 时间格式字符串或false,超出范围时返回指定格式,否则返回多久以前
* @returns 格式化后的时间字符串
*/
export function timeFrom(dateTime: number | string | Date | null = null, format: string | false = 'yyyy-mm-dd'): string {
// 如果为null,则格式化当前时间
if (!dateTime)
dateTime = Number(new Date())
// 如果dateTime长度为10或者13,则为秒和毫秒的时间戳,如果超过13位,则为其他的时间格式
if (typeof dateTime === 'number' || typeof dateTime === 'string') {
if (dateTime.toString().length === 10)
dateTime = Number(dateTime) * 1000
}
const timestamp = +new Date(Number(dateTime))
const timer = (Number(new Date()) - timestamp) / 1000
// 如果小于5分钟,则返回"刚刚",其他以此类推
let tips = ''
switch (true) {
case timer < 300:
tips = '刚刚'
break
case timer >= 300 && timer < 3600:
tips = `${Number.parseInt(String(timer / 60))}分钟前`
break
case timer >= 3600 && timer < 86400:
tips = `${Number.parseInt(String(timer / 3600))}小时前`
break
case timer >= 86400 && timer < 2592000:
tips = `${Number.parseInt(String(timer / 86400))}天前`
break
default:
// 如果format为false,则无论什么时间戳,都显示xx之前
if (format === false) {
if (timer >= 2592000 && timer < 365 * 86400) {
tips = `${Number.parseInt(String(timer / (86400 * 30)))}个月前`
}
else {
tips = `${Number.parseInt(String(timer / (86400 * 365)))}年前`
}
}
else {
tips = timeFormat(timestamp, format as string)
}
}
return tips
}
export default timeFrom