go语言中CreatedAt时间格式的处理

怎么将gorm.Model中的CreatedAt转变成年月日时分秒的形式

在 GORM 中,gorm.Model 中的 CreatedAt 字段是 time.Time 类型。为了将它转换成年月日时分秒的格式,可以使用 Go 的时间格式化函数 Format,它可以将 time.Time 类型的时间格式化为指定的字符串形式。
Go 使用特定的参考时间来指定格式:2006-01-02 15:04:05。你可以根据需要的时间格式调整该模板。
示例:将 CreatedAt 转换为年月日时分秒格式
假设你有一个结构体 Article 继承了 gorm.Model,你可以这样格式化 CreatedAt 字段:

package main

import (
    "fmt"
    "time"
    "gorm.io/gorm"
)

// Article 结构体包含 gorm.Model
type Article struct {
    gorm.Model
    Title string
}

func main() {
    // 假设有一个 Article 实例
    article := Article{
        Title: "Example Title",
        Model: gorm.Model{
            CreatedAt: time.Now(),
        },
    }

    // 格式化 CreatedAt 为 年-月-日 时:分:秒
    formattedTime := article.CreatedAt.Format("2006-01-02 15:04:05")
    fmt.Println("CreatedAt (formatted):", formattedTime)
}

结果:
假设 CreatedAt 是当前时间,格式化后的输出可能是这样的:
CreatedAt (formatted): 2024-09-30 14:35:20

解释:

  • article.CreatedAt.Format(“2006-01-02 15:04:05”):使用 Format() 方法将 CreatedAt 转换为你想要的格式。2006-01-02 15:04:05 是 Go 的固定模板,代表年-月-日 时:分:秒的格式。
  • time.Now():获取当前时间,模拟 CreatedAt 字段的值。
    其他常见的时间格式化示例:
  • YYYY-MM-DD: “2006-01-02”
  • DD-MM-YYYY HH:MM:SS: “02-01-2006 15:04:05”
  • YYYY/MM/DD HH:MM: “2006/01/02 15:04”
    根据需要,你可以调整 Format() 中的模板以得到不同的格式。
    总结:
    要将 gorm.Model 中的 CreatedAt 转换为年月日时分秒格式,可以使用 time.Time 的 Format() 方法,并传入合适的格式化模板 (“2006-01-02 15:04:05”)。

示例:

写一个函数,参数为一个time.Time类型的值,返回值为string类型,实现得出现在time.Time类型的值,和传入参数的值得差值,然后根据差值去计算,如果差值大于0,小于60秒,就输出几秒前,如果差值小于1小时,就输出几分钟前,如果差值大于1小时,小于24小时,就输出几小时前,如果差值大于24小时,小于30天,就输出几天前,如果差值大于30天,小于12个月,就输出几个月前,如果大于12个月,就输出几年前。

要实现一个函数,根据传入的 time.Time 值与当前时间的差异,输出类似“几秒前”、“几分钟前”这样的格式,可以使用 time.Since() 函数计算时间差,并根据差值的大小进行条件判断。
下面是具体的实现代码:

package main

import (
    "fmt"
    "time"
)

// TimeAgo 函数根据传入的 time.Time 和当前时间计算差值并返回相应的时间描述
func TimeAgo(t time.Time) string {
    duration := time.Since(t) // 计算传入时间和当前时间的差值

    seconds := int(duration.Seconds())
    minutes := int(duration.Minutes())
    hours := int(duration.Hours())
    days := hours / 24
    months := days / 30
    years := days / 365

    if seconds < 60 {
        return fmt.Sprintf("%d秒前", seconds)
    } else if minutes < 60 {
        return fmt.Sprintf("%d分钟前", minutes)
    } else if hours < 24 {
        return fmt.Sprintf("%d小时前", hours)
    } else if days < 30 {
        return fmt.Sprintf("%d天前", days)
    } else if months < 12 {
        return fmt.Sprintf("%d个月前", months)
    } else {
        return fmt.Sprintf("%d年前", years)
    }
}

func main() {
    // 测试当前时间 5 分钟前
    pastTime := time.Now().Add(-5 * time.Minute)
    fmt.Println(TimeAgo(pastTime))

    // 测试当前时间 3 小时前
    pastTime = time.Now().Add(-3 * time.Hour)
    fmt.Println(TimeAgo(pastTime))

    // 测试当前时间 2 天前
    pastTime = time.Now().Add(-2 * 24 * time.Hour)
    fmt.Println(TimeAgo(pastTime))

    // 测试当前时间 45 天前
    pastTime = time.Now().Add(-45 * 24 * time.Hour)
    fmt.Println(TimeAgo(pastTime))

    // 测试当前时间 13 个月前
    pastTime = time.Now().Add(-13 * 30 * 24 * time.Hour)
    fmt.Println(TimeAgo(pastTime))
}

解释:

  • time.Since(t):计算传入时间 t 到现在的时间差,返回一个 time.Duration 类型的值。
  • duration.Seconds()、duration.Minutes()、duration.Hours():将时间差转换为秒、分钟、小时等单位。
  • 条件判断:根据时间差大小,判断是否是“秒前”、“分钟前”、“小时前”等,输出相应的描述。
    示例输出:
    5分钟前
    3小时前
    2天前
    1个月前
    1年前

处理逻辑:

  • 如果时间差小于 60 秒,输出 x 秒前。
  • 如果时间差小于 60 分钟,输出 x 分钟前。
  • 如果时间差小于 24 小时,输出 x 小时前。
  • 如果时间差小于 30 天,输出 x 天前。
  • 如果时间差小于 12 个月,输出 x 个月前。
  • 如果时间差大于等于 12 个月,输出 x 年前。
    这样你可以根据传入的时间值,得到一个人类可读的时间差描述。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值