保洁阿姨
保洁阿姨

接前后端定制开发

时间过滤器(工具类)

方法源码

/**
 * 时间过滤器
 */
export function timestamp(datetime: string): string {
    const timestamp = new Date(datetime).getTime()
    const diff = Date.now() - timestamp
    const minute = 60 * 1000
    const hour = 60 * minute
    const day = 24 * hour
    const week = 7 * day
    const month = 30 * day
    const year = 365 * day
    if (diff < minute) {
        return '刚刚'
    } else if (diff < hour) {
        const minutes = Math.floor(diff / minute)
        return `${minutes} 分钟前`
    } else if (diff < day) {
        const hours = Math.floor(diff / hour)
        return `${hours} 小时前`
    } else if (diff < week) {
        const days = Math.floor(diff / day)
        return `${days} 天前`
    } else if (diff < month) {
        const weeks = Math.floor(diff / week)
        return `${weeks} 周前`
    } else if (diff < year) {
        const months = Math.floor(diff / month)
        return `${months} 个月前`
    } else {
        const years = Math.floor(diff / year)
        return `${years} 年前`
    }
}

使用示例

timestamp(2023-5-27 10:20:55)

点赞(0)

收藏(0)

打赏

发布于

发表评论- Comments

表情
预览

您必须登录注册发表评论

暂无数据
up↑

用户签到

签到
点击签到赚积分

- 今日签到 -

今日暂无签到

精彩视频

最近访客

解决你遇到的所有问题