Skip to content
鼓励作者:欢迎打赏犒劳

时间工具类

分区时间

获取不同分区的时间

go
package main

import (
	"fmt"
	"testing"
	"time"
)

func TestTime(t *testing.T) {
	fmt.Println("本机 时间:", time.Now().Format("2006-01-02 15:04:05"))

	utcNow := time.Now().UTC()
	fmt.Println("当前 UTC 时间:", utcNow.Format("2006-01-02 15:04:05"))

	/**
	本机 时间: 2025-09-29 22:41:30
	当前 UTC 时间: 2025-09-29 14:41:30
	*/
}

不同时区转换

go
// 加载 UTC 时区
loc, err := time.LoadLocation("UTC") // 东八区 Asia/Shanghai
if err != nil {
    panic(err) // 正常情况下不会出错
}

// 获取指定时区的时间
utcNow := time.Now().In(loc)
fmt.Println("当前 UTC 时间:", utcNow.Format("2006-01-02 15:04:05"))

自定义时区转换

就是你可以任意设置偏移量。

go
// 创建一个名为 "UTC",偏移为 0 秒的时区
utcZone := time.FixedZone("UTC", 0)

// 获取指定时区的时间
utcNow := time.Now().In(utcZone)
fmt.Println("当前 UTC 时间:", utcNow.Format("2006-01-02 15:04:05"))

时间比较

go
package main

import (
	"fmt"
	"time"
)

func main() {
	createAt := time.Now().Add(-25 * time.Hour) // 25小时前
	now := time.Now()
	if now.Sub(createAt) > 24*time.Hour {
		fmt.Println("超过24小时") // 会输出
	}

}

时间转换

时间戳&time互转

go
package main

import (
	"fmt"
	"time"
)

func main() {
	//time 转 时间戳>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	// 获取当前时间
	now := time.Now()

	// 转换为秒级时间戳
	timestampSec := now.Unix()
	// 转换为毫秒级时间戳
	timestampMilli := now.UnixMilli() // Go 1.17+

	fmt.Printf("当前时间: %v\n", now)
	fmt.Printf("秒级时间戳: %d\n", timestampSec)
	fmt.Printf("毫秒级时间戳: %d\n", timestampMilli)

	fmt.Println("=====================================")
	//时间戳转time>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	// 假设有一个秒级时间戳
	sec := int64(1700000000)
	milli := int64(1700000000500)

	// 秒级时间戳 → time.Time
	t1 := time.Unix(sec, 0)

	// 毫秒级时间戳 → time.Time
	t2 := time.Unix(milli/1000, 0)

	fmt.Println("秒时间戳转时间:", t1)
	fmt.Println("毫秒时间戳转时间:", t2)

	/**
	当前时间: 2025-09-24 23:16:59.4266434 +0800 CST m=+0.000000001
	秒级时间戳: 1758727019
	毫秒级时间戳: 1758727019426
	=====================================
	秒时间戳转时间: 2023-11-15 06:13:20 +0800 CST
	毫秒时间戳转时间: 2023-11-15 06:13:20 +0800 CST
	*/
}

time&字符串互转

go
package main

import (
	"fmt"
	"time"
)

func main() {
	//时间 → 字符串(格式化)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	now := time.Now()

	// 格式化输出
	fmt.Println(now.Format("2006-01-02"))                // yyyy-MM-dd
	fmt.Println(now.Format("2006-01-02 15:04:05"))       // yyyy-MM-dd HH:mm:ss
	fmt.Println(now.Format("2006/01/02 15:04:05"))       // 自定义分隔符
	fmt.Println(now.Format("2006-01-02T15:04:05Z07:00")) // ISO8601 / RFC3339

	fmt.Println("=====================================")
	//字符串 → 时间(解析)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	// 字符串转时间
	dateStr1 := "2024-01-01"
	dateStr2 := "2024-01-01 12:30:45"

	t1, err := time.Parse("2006-01-02", dateStr1)
	if err != nil {
		panic(err)
	}

	t2, err := time.Parse("2006-01-02 15:04:05", dateStr2)
	if err != nil {
		panic(err)
	}

	fmt.Println("解析日期:", t1)
	fmt.Println("解析日期时间:", t2)

	/**
	2025-09-24
	2025-09-24 23:19:04
	2025/09/24 23:19:04
	2025-09-24T23:19:04+08:00
	=====================================
	解析日期: 2024-01-01 00:00:00 +0000 UTC
	解析日期时间: 2024-01-01 12:30:45 +0000 UTC
	*/
}

工具类

go
package main

import (
	"fmt"
	"time"
)

// TimeToTimestampSec 时间转秒级时间戳
func TimeToTimestampSec(t time.Time) int64 {
	return t.Unix()
}

// TimeToTimestampMilli 时间转毫秒级时间戳
func TimeToTimestampMilli(t time.Time) int64 {
	return t.UnixMilli()
}

// TimestampToTime 秒级时间戳转 time.Time
func TimestampToTime(sec int64) time.Time {
	return time.Unix(sec, 0)
}

// MilliTimestampToTime 毫秒级时间戳转 time.Time
func MilliTimestampToTime(milli int64) time.Time {
	return time.Unix(milli/1000, (milli%1000)*1e6)
}

// FormatDate 格式化为 yyyy-MM-dd
func FormatDate(t time.Time) string {
	return t.Format("2006-01-02")
}

// FormatDateTime 格式化为 yyyy-MM-dd HH:mm:ss
func FormatDateTime(t time.Time) string {
	return t.Format("2006-01-02 15:04:05")
}

// ParseDate 解析 yyyy-MM-dd
func ParseDate(s string) (time.Time, error) {
	return time.Parse("2006-01-02", s)
}

// ParseDateTime 解析 yyyy-MM-dd HH:mm:ss
func ParseDateTime(s string) (time.Time, error) {
	return time.Parse("2006-01-02 15:04:05", s)
}

func main() {
	now := time.Now()

	fmt.Println("原始时间:", now)
	fmt.Println("秒时间戳:", TimeToTimestampSec(now))
	fmt.Println("毫秒时间戳:", TimeToTimestampMilli(now))
	fmt.Println("格式化日期:", FormatDate(now))
	fmt.Println("格式化日期时间:", FormatDateTime(now))

	t, _ := ParseDateTime("2024-12-25 10:30:00")
	fmt.Println("解析时间:", t)

	/***
	原始时间: 2025-09-24 23:21:02.3874178 +0800 CST m=+0.000000001
	秒时间戳: 1758727262
	毫秒时间戳: 1758727262387
	格式化日期: 2025-09-24
	格式化日期时间: 2025-09-24 23:21:02
	解析时间: 2024-12-25 10:30:00 +0000 UTC
	*/
}

如有转载或 CV 的请标注本站原文地址