Golang

关注公众号 jb51net

关闭
首页 > 脚本专栏 > Golang > Golang time.Format时间格式化

Golang time.Format时间格式化的实现

作者:穿越時空

Go语言的time.Format使用一个固定的参考时间来定义格式,本文就来介绍一下Golang time.Format时间格式化的实现,感兴趣的可以了解一下

Go 语言的 time.Format 使用一个固定的参考时间 Mon Jan 2 15:04:05 MST 2006 来定义格式,这个时间是 2006-01-02 15:04:05,对应数字 1 2 3 4 5 6 7(月份=1,日期=2,小时=15=3,分钟=4,秒=5,年份=6,时区=7),便于记忆。

一、核心原理

package main
import (
    "fmt"
    "time"
)
func main() {
    t := time.Date(2026, 4, 21, 9, 6, 0, 0, time.Local)
    // 使用参考时间定义格式
    fmt.Println(t.Format("2006-01-02 15:04:05"))      // 2026-04-21 09:06:00
    fmt.Println(t.Format("2006/01/02"))               // 2026/04/21
    fmt.Println(t.Format("Jan 2, 2006"))              // Apr 21, 2026
    fmt.Println(t.Format("Monday, January 2, 2006"))  // Tuesday, April 21, 2026
}

二、常用格式速查

格式代码输出示例
标准日期时间"2006-01-02 15:04:05"2026-04-21 09:06:00
紧凑日期时间"20060102150405"20260421090600
仅日期"2006-01-02"2026-04-21
仅时间"15:04:05"09:06:00
12小时制"2006-01-02 03:04:05 PM"2026-04-21 09:06:00 AM
RFC3339time.RFC33392026-04-21T09:06:00+08:00
RFC1123time.RFC1123Tue, 21 Apr 2026 09:06:00 CST
中文格式"2006年01月02日 15时04分05秒"2026年04月21日 09时06分00秒
日志格式"2006/01/02 - 15:04:05.000"2026/04/21 - 09:06:00.000

三、预定义常量

time 包内置了常用格式常量:

const (
    ANSIC       = "Mon Jan _2 15:04:05 2006"
    UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
    RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
    RFC822      = "02 Jan 06 15:04 MST"
    RFC822Z     = "02 Jan 06 15:04 -0700"
    RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
    RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
    RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700"
    RFC3339     = "2006-01-02T15:04:05Z07:00"
    RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
    Kitchen     = "3:04PM"
    Stamp       = "Jan _2 15:04:05"
    StampMilli  = "Jan _2 15:04:05.000"
    StampMicro  = "Jan _2 15:04:05.000000"
    StampNano   = "Jan _2 15:04:05.000000000"
)

使用示例:

t := time.Now()
fmt.Println(t.Format(time.RFC3339))      // 2026-04-21T09:06:00+08:00
fmt.Println(t.Format(time.Kitchen))      // 9:06AM

四、解析时间(Parse)

time.Parse 使用相同的参考时间格式:

// 解析字符串为 time.Time
t, err := time.Parse("2006-01-02 15:04:05", "2026-04-21 09:06:00")
if err != nil {
    log.Fatal(err)
}
// 带时区解析
t, err = time.ParseInLocation("2006-01-02 15:04:05", "2026-04-21 09:06:00", time.Local)

五、常见坑

1. 月份大小写

"01"  // 月份数字 (01-12)
"1"   // 月份数字 (1-12,无前导零)
"Jan" // 英文缩写 (Jan, Feb...)
"January" // 英文全名

2. 日期大小写

"02"  // 日期 (01-31)
"2"   // 日期 (1-31)
"_2"  // 日期 ( 1-31,空格补齐)

3. 小时制

"15"  // 24小时制 (00-23)
"03"  // 12小时制 (01-12)
"3"   // 12小时制 (1-12)
"PM"  // 大写 AM/PM
"pm"  // 小写 am/pm

4. 时区

"MST"     // 时区缩写 (CST, PST...)
"-0700"   // 数字时区偏移
"-07:00"  // 带冒号的时区偏移
"Z0700"   // RFC3339 格式时区
"Z07:00"  // RFC3339 带冒号

六、实用工具函数

package main
import (
    "fmt"
    "time"
)
// 常用格式常量
const (
    FormatDateTime = "2006-01-02 15:04:05"
    FormatDate     = "2006-01-02"
    FormatTime     = "15:04:05"
    FormatCompact  = "20060102150405"
)
// FormatTime 格式化时间
func FormatTime(t time.Time) string {
    return t.Format(FormatDateTime)
}
// ParseTime 解析时间字符串
func ParseTime(s string) (time.Time, error) {
    return time.ParseInLocation(FormatDateTime, s, time.Local)
}
// BeginOfDay 获取当天开始时间
func BeginOfDay(t time.Time) time.Time {
    return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
// EndOfDay 获取当天结束时间
func EndOfDay(t time.Time) time.Time {
    return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 999999999, t.Location())
}
func main() {
    now := time.Now()
    fmt.Println("当前时间:", FormatTime(now))
    fmt.Println("当天开始:", BeginOfDay(now).Format(FormatDateTime))
    fmt.Println("当天结束:", EndOfDay(now).Format(FormatDateTime))
    t, _ := ParseTime("2026-04-21 09:06:00")
    fmt.Println("解析结果:", t.Format(FormatDateTime))
}

七、记忆口诀

月份 1,日期 2,小时 3(15点),分钟 4,秒 5,年份 6,时区 7
2006-01-02 15:04:05
     ↑  ↑   ↑  ↑  ↑
     6  1   3  4  5

记住这个参考时间,所有格式都基于此推导即可。

到此这篇关于Golang time.Format时间格式化的实现的文章就介绍到这了,更多相关Golang time.Format时间格式化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文