Golang时间

时间:2025-03-02 08:18:13

文章目录

        • 当前时间
        • 时间格式化
        • 字符串转时间
        • 时间转时间戳
        • 字符串转时间戳
        • 时间向前或向后
        • 获取两个时间的间隔(持续时间)

当前时间

golang 的时间是

package main

import (
	"fmt"
	"time"
)

func main() {
 //当前时间 golang的时间是 
 fmt.Println(time.Now()) //2022-11-03 19:52:12.457665 +0800 CST m=+0.000146886
}

时间格式化

(layout string) 进行格式化 layout:格式 2006:年 01:月 02:日 15:时 04:分 05:秒

package main

import (
	"fmt"
	"time"
)

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

 //(layout string) layout:格式 2006:年 01:月 02:日 15:时 04:分 05:秒
 fmt.Println(now.Format("2006-01-02 15:04:05")) //2022-11-04 10:33:40
 fmt.Println(now.Format("2006-01-02")) //2022-11-04
 fmt.Println(now.Format("15:04:05")) //10:33:40

}

字符串转时间

(layout, value string) (Time, error) 将字符串时间转换成,其中layout是时间格式,value是字符串时间,要求value格式匹配layout,使用默认UTC时区

和 基本一致,只不过提供设置时区(第三参数)
根据本地系统区获取对应的时区
(name string) (*Location, error) 根据name查找对应的时区

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"

	// (layout, value string) (Time, error) 将字符串时间转换成,其中layout是时间格式,value是字符串时间,要求value格式匹配layout,使用默认UTC时区
	parse, err := time.Parse("2006-01-02 15:04:05", value)
	if err != nil {
		panic(err)
	}
	fmt.Println(parse) //2022-02-02 22:22:22 +0000 UTC
	parseAndPrintTime(format,value,"") // 2022-02-02 22:22:22 +0800 CST
	parseAndPrintTime(format,value,"Japan") // 2022-02-02 22:22:22 +0900 JST
}

func parseAndPrintTime(layout, value, location string) {
	// 和  基本一致,只不过提供设置时区(第三参数)
	// 根据本地系统区获取对应的时区
	//(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	fmt.Println(t)
}

时间转时间戳

使用()将时间转换成unix时间戳,注意时间戳是UTC标准下的时间戳

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"
	parse, err := time.Parse(format, value)
	if err != nil {
		panic(err)
	}

	parse2 := parseAndTime(format,value,"PRC")
	fmt.Println(parse) //2022-02-02 22:22:22 +0000 UTC
	fmt.Println(parse2) //2022-02-02 22:22:22 +0800 CST

	//使用()将时间转换成unix时间戳,注意时间戳是UTC标准下的时间戳
	fmt.Println(parse.Unix()) // 1643840542
	fmt.Println(parse2.Unix()) // 1643811742
}

func parseAndTime(layout, value, location string) time.Time{
	// 和  基本一致,只不过提供设置时区(第三参数)
	// 根据本地系统区获取对应的时区
	//(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
字符串转时间戳

1.字符串先转时间,时间再转时间戳

是转换成当地时区的时间,所以要注意时间的时区,可能出现显示的时间和预期的不一样的结果

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"
	parse, err := time.Parse(format, value)
	if err != nil {
		panic(err)
	}

	parse2 := parseAndTime(format,value,"PRC")
	fmt.Println(parse) //2022-02-02 22:22:22 +0000 UTC
	fmt.Println(parse2) //2022-02-02 22:22:22 +0800 CST

	//使用()将时间转换成unix时间戳,注意时间戳是UTC标准下的时间戳
	fmt.Println(parse.Unix()) // 1643840542
	fmt.Println(parse2.Unix()) // 1643811742

	unix := time.Unix(parse.Unix(), 0)
	unix2 := time.Unix(parse2.Unix(), 0)

	//是转换成当地时区的时间,所以要注意时间的时区,可能出现显示的时间和预期的不一样的结果
	fmt.Println(unix.Format(format)) //2022-02-03 06:22:22 (PRC比UTC早8个小时)
	fmt.Println(unix2.Format(format))  //2022-02-02 22:22:22
}

func parseAndTime(layout, value, location string) time.Time{
	// 和  基本一致,只不过提供设置时区(第三参数)
	// 根据本地系统区获取对应的时区
	//(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
时间向前或向后

时间的前进和后退可以使用(),正的往后推,负的往前推

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value := "2022-02-02 22:22:22"
	t := parseAndTime(format,value,"PRC")

	//时间的前进和后退可以使用(),正的往后推,负的往前推
	t2 := t.Add(time.Hour * 12)
	t3 := t.Add(time.Hour * -12)
	fmt.Println(t2) // 2022-02-03 10:22:22 +0800 CST
	fmt.Println(t3) // 2022-02-02 10:22:22 +0800 CST
}

func parseAndTime(layout, value, location string) time.Time{
	// 和  基本一致,只不过提供设置时区(第三参数)
	// 根据本地系统区获取对应的时区
	//(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}
获取两个时间的间隔(持续时间)

(time2) 获取time - time2的间隔(持续时间:)

package main

import (
	"fmt"
	"time"
)

func main() {
	format := "2006-01-02 15:04:05"
	value1 := "2022-02-02 22:22:22"
	value2 := "2022-02-02 12:10:10"
	t1 := parseAndTime(format,value1,"PRC")
	t2 := parseAndTime(format,value2,"PRC")
	//(time2) 获取time - time2的间隔(持续时间:)
	fmt.Println(t1.Sub(t2)) //10h12m12s
}

func parseAndTime(layout, value, location string) time.Time{
	// 和  基本一致,只不过提供设置时区(第三参数)
	// 根据本地系统区获取对应的时区
	//(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}

1.可以理解为时间中比较常用的“单位”,常用的有:时(1h0m0s),分(1m0s),秒(1s)
2.如果需要一些特别的持续时间,可以通过 (s string) (Duration, error) 将时间解析为

package main

import (
	"fmt"
	"time"
)

func main() {
	//可以理解为时间中比较常用的“单位”,常用的有:时(h),分(m),秒(s)
	fmt.Println(time.Second) //1s
	fmt.Println(time.Hour) //1h0m0s
	fmt.Println(time.Minute) //1m0s

	//如果需要一些特别的持续时间,可以通过 (s string) (Duration, error) 将时间解析为
	duration, err := time.ParseDuration("5h20m")
	if err != nil {
		panic(err)
	}
	fmt.Println(duration) //5h20m0s
}

func parseAndTime(layout, value, location string) time.Time{
	// 和  基本一致,只不过提供设置时区(第三参数)
	// 根据本地系统区获取对应的时区
	//(name string) (*Location, error) 根据name查找对应的时区
	var loadLocation *time.Location
	if location == "" {
		loadLocation = time.Local
	}else {
		l, err := time.LoadLocation(location)
		if err != nil {
			panic(err)
		}
		loadLocation = l
	}

	t, err := time.ParseInLocation(layout, value, loadLocation) //:根据系统找到对应时区
	if err != nil {
		panic(err)
	}
	return t
}