Golang学习篇——UTC时间互换标准时间

Golang时间相关处理,相关包 "time"

1. UTC时间转标准时间

1
2
3
4
5
//UTC时间转标准时间
func (this *DataSearch) UTCTransLocal(utcTime string) string {
    t, _ := time.Parse("2006-01-02T15:04:05.000+08:00", utcTime)
    return t.Local().Format("2006-01-02 15:04:05")
}

调用结果: 2020-04-29 22:11:08

1
2
t1 := UTCTransLocal("2020-04-29T14:11:08.000+08:00")
fmt.Println(t1)

2. 标准时间转UTC时间

1
2
3
4
5
//标准时间转UTC时间
func (this *DataSearch) LocalTransUTC(localTime string) string {
    t, _ := time.ParseInLocation("2006-01-02 15:04:05", localTime, time.Local)
    return t.UTC().Format("2006-01-02T15:04:05.000+08:00")
}

调用结果: 2020-04-29T14:11:08.000+08:00

1
2
t2 := LocalTransUTC("2020-04-29 22:11:08")
fmt.Println(t2)

3. str格式化时间

1
2
3
//格式化时间格式, 据说是Go诞生之日, 口诀:6-1-2-3-4-5
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
//2020-04-30 13:15:02

4. str格式化时间转时间戳

1
2
3
4
5
6
7
the_time, err := time.Parse("2006-01-02 15:04:05", "2020-04-29 22:11:08")
if err == nil {
    unix_time := the_time.Unix()
    fmt.Println(unix_time)
}
fmt.Println(the_time)
//1588198268

5.时间戳转str格式化时间

1
2
3
str_time := time.Unix(1588224111, 0).Format("2006-01-02 15:04:05")
fmt.Println(str_time)
//2020-04-30 13:21:51