Go时间处理汇总

当前时间戳

now := time.Now()
fmt.Println(now.Unix()) // 1651416636

时间格式化 

now := time.Now()
fmt.Println(time.Now().Format("2006-01-02 15:04:05")) // 2022-05-01 22:50:36
fmt.Println(time.Now().Format("06-1-2 3:4:5")) // 22-5-1 10:50:36

指定Str转Time

t := time.Date(2022, 5, 1, 0, 0, 0, 0, time.Local)
fmt.Println(t.Format("2006-01-02 15:04:05")) // 2022-05-01 00:00:00

今日开始时间和结束时间 

now := time.Now()
startTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
endTime := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 999999999, now.Location())
fmt.Println(startTime.Unix(), startTime.Format("2006-01-02 15:04:05")) // 1651334400 2022-05-01 00:00:00
fmt.Println(endTime.Unix(), endTime.Format("2006-01-02 15:04:05")) // 1651420799 2022-05-01 23:59:59

 返回年月日 时分秒 纳秒

now := time.Now()
fmt.Printf("year:%d, month:%d, day:%d, hour:%d, minute:%d, second:%d, nanosecond:%d\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), now.Nanosecond()) 
// year:2022, month:5, day:1, hour:23, minute:19, second:57, nanosecond:71963600

year, month, day := now.Date()      // 年月日
hour, minute, second := now.Clock() // 时分秒
fmt.Printf("year:%d, month:%d, day:%d, hour:%d, minute:%d, second:%d\n", year, month, day, hour, minute, second) 
// year:2022, month:5, day:1, hour:23, minute:19, second:57

返回星期几 

now := time.Now()
fmt.Printf("The day of the week:%d\n", now.Weekday()) // The day of the week:0
fmt.Printf("The day of the week:%s\n", now.Weekday()) // The day of the week:Sunday 

返回一年中第几天

now := time.Now()
fmt.Printf("The day of the year:%d\n", now.YearDay()) // The day of the year:121

返回时区

now := time.Now()
fmt.Printf("The time zone:%s\n", now.Location()) // The time zone:Local

多少时间之前之后

now := time.Now()

// such as "300ms", "-1.5h" or "2h45m".  Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

// 10小时之前(单位: "ns", "us", "ms", "s", "m", "h")
m1, _ := time.ParseDuration("-10h")
fmt.Printf("Before 10 hours:%s\n", now.Add(m1).Format("2006-01-02 15:04:05")) // Before 10 hours:2022-05-01 13:48:56

// 10小时之后(单位: "ns", "us", "ms", "s", "m", "h")
m2, _ := time.ParseDuration("10h")
fmt.Printf("After 10 hours:%s\n", now.Add(m2).Format("2006-01-02 15:04:05")) // After 10 hours:2022-05-02 09:48:56

// 10天之前(单位: "years", "months", "days")
fmt.Printf("Before 10 days:%s\n", now.AddDate(0, 0, -10).Format("2006-01-02 15:04:05")) // Before 10 days:2022-04-21 23:48:56

// 10天之后(单位: "years", "months", "days")
fmt.Printf("After 10 days:%s\n", now.AddDate(0, 0, 10).Format("2006-01-02 15:04:05")) // After 10 days:2022-05-11 23:48:56

判断一个时间是另一个时间之前之后

time1 := time.Now() // 2022-05-01 23:53:21.5464799 +0800 CST
time2 := now.AddDate(0, 0, -1) // 2022-04-30 23:53:21.5464799 +0800 CST
fmt.Printf("%v\n", time1.Before(time2)) // false
fmt.Printf("%v\n", time1.After(time2)) // true

判断一个时间 相比另外一个时间过了多久

扫描二维码关注公众号,回复: 14723905 查看本文章
now := time.Now()
time.Sleep(time.Second * 3)
fmt.Println(time.Since(now).Seconds()) // 3.0129251

时间差友好显示 

package main

import (
	"fmt"
	"math"
	"time"
)

func main() {

	startTime := time.Now()
	endTime := time.Now().Add(time.Second * 10000)

	fmt.Printf("%+v", FriendlyDiffTimeFormat(startTime, endTime)) // 2h 46m 40s
}

func FriendlyDiffTimeFormat(startTime time.Time, endTime time.Time) string {

	SubTime := int(math.Abs(endTime.Sub(startTime).Seconds()))

	// 秒
	if SubTime < 60 {
		return fmt.Sprintf("%ds", SubTime)
	}

	// 分钟
	if SubTime < 60*60 {
		minute := int(math.Floor(float64(SubTime / 60)))
		second := SubTime % 60
		return fmt.Sprintf("%dm %ds", minute, second)
	}

	// 小时
	if SubTime < 60*60*24 {
		hour := int(math.Floor(float64(SubTime / (60 * 60))))
		tail := SubTime % (60 * 60)
		minute := int(math.Floor(float64(tail / 60)))
		second := tail % 60
		return fmt.Sprintf("%dh %dm %ds", hour, minute, second)
	}

	// 天
	day := int(math.Floor(float64(SubTime / (60 * 60 * 24))))
	tail := SubTime % (60 * 60 * 24)
	hour := int(math.Floor(float64(tail / (60 * 60))))
	tail = SubTime % (60 * 60)
	minute := int(math.Floor(float64(tail / 60)))
	second := tail % 60

	return fmt.Sprintf("%dd %dh %dm %ds", day, hour, minute, second)
}

猜你喜欢

转载自blog.csdn.net/qq_34272964/article/details/124534520