Go time包时间的常用方法

 基础语法  2022-02-04  admin  1072  1447

Go time包时间的常用方法,时间戳,时间格式化,时间计算

package main

import (
	"fmt"
	"time"
)

func main() {
	// 获取指定时间戳的标准格式日期时间
	str := time.Unix(1, 0) // 1970-01-01 08:00:01 +0800 CST
	fmt.Println(str)
	str = time.Unix(1636762290, 200) // 2021-11-13 08:11:30.0000002 +0800 CST
	fmt.Println(str)
	// 获取现在的标准格式日期时间
	str = time.Now() // 2021-11-13 08:14:40.4277194 +0800 CST m=+0.035915201
	fmt.Println(str)
	// 获取指定日期时间的标准格式日期时间
	str = time.Date(2006, 1, 2, 15, 04, 05, 0, time.UTC) // 2006-01-02 15:04:05 +0000 UTC
	fmt.Println(str)

	// 时区(location)
	// 默认UTC
	loc, _ := time.LoadLocation("") // UTC
	fmt.Println(loc)
	// Local
	loc, _ = time.LoadLocation("Local") // Local
	fmt.Println(loc)
	// Asia/Shanghai  所有时区 /time/zoneinfo_abbrs_windows.go
	loc, _ = time.LoadLocation("Asia/Shanghai")     // Asia/Shanghai
	str = time.Date(2006, 1, 2, 15, 04, 05, 0, loc) // 2006-01-02 15:04:05 +0800 CST  /// CST China Standard Time
	fmt.Println(str)

	// 格式化日期时间 2006-01-02 15:04:05
	str2 := time.Date(2008, 10, 1, 20, 30, 10, 0, loc).Format("2006/01/02 15时04分05秒")
	fmt.Println(str2) // 2008/10/01 20时30分10秒

	fmt.Println(time.Now().Year())       // 2021
	fmt.Println(time.Now().Month())      // November
	fmt.Println(time.Now().Day())        // 13
	fmt.Println(time.Now().Hour())       // 9
	fmt.Println(time.Now().Minute())     // 12
	fmt.Println(time.Now().Second())     // 43
	fmt.Println(time.Now().Nanosecond()) // 741133200

	// 解析日期字符串
	t, _ := time.Parse("2006年1月2日 15h04m05s", "") // 0001-01-01 00:00:00 +0000 UTC
	fmt.Println(t)

	t, _ = time.Parse("2006年1月2日 15h04m05s", "2021年10月1日 13h10m09s") // 2021-10-01 13:10:09 +0000 UTC
	fmt.Println(t)

	t, _ = time.Parse("2006-01-02 15:04:05", "2021-10-01 01:02:03") // 2021-10-01 01:02:03 +0000 UTC
	fmt.Println(t)

	//现在获取时间戳
	timestamp := time.Now().Unix() // 1636887943
	fmt.Println(timestamp)
	timestamp = time.Now().UnixNano() // 1636887943826710000
	fmt.Println(timestamp)

	//获取指定时间的时间戳
	timestamp = time.Date(2008, 10, 1, 13, 14, 15, 0, loc).Unix() // 1222838055
	fmt.Println(timestamp)
	timestamp = time.Date(2008, 10, 1, 13, 14, 15, 0, loc).UnixNano() // 1222838055000000000
	fmt.Println(timestamp)
	t, _ = time.Parse("2006-01-02 15:04:05", "2021-10-01 01:02:03") // 2021-10-01 01:02:03 +0000 UTC
	timestamp = t.Unix()                                            // 1633050123
	fmt.Println(timestamp)

	//时间计算
	fmt.Println("====")
	d := time.Duration(2*24) * time.Second
	fmt.Println(d) //48s
	d = time.Duration(2*24) * time.Hour
	fmt.Println(d) //48h0m0s
	d = 2 * 24 * time.Hour
	fmt.Println(d) //48h0m0s

	timestamp = time.Date(2008, 10, 1, 13, 14, 15, 0, loc).Unix() + 3600 // 1222838055
	str3 := time.Unix(timestamp, 0).Format("2006-01-02 15:04:05")        //2008-10-01 14:14:15

	str3 = time.Date(2008, 10, 1, 13, 14, 15, 0, loc).Add(3600 * 1000000000).Format("2006-01-02 15:04:05") // 2008-10-01 14:14:15
	fmt.Println(str3)
	str3 = time.Date(2008, 10, 1, 13, 14, 15, 0, loc).AddDate(0, 1, 0).Format("2006-01-02 15:04:05") // 2008-11-01 13:14:15
	fmt.Println(str3)

	p, _ := time.ParseDuration("10h")
	str3 = time.Date(2008, 10, 1, 13, 14, 15, 0, loc).Add(p).Format("2006-01-02 15:04:05") // 2008-10-01 23:14:15
	fmt.Println(str3)

	p, _ = time.ParseDuration("-10h")
	str3 = time.Date(2008, 10, 1, 13, 14, 15, 0, loc).Add(p).Format("2006-01-02 15:04:05") // 2008-10-01 03:14:15
	fmt.Println(str3)

	fmt.Println(p.Minutes()) // -600
	fmt.Println(p.String())  // -10h0m0s

	t, _ = time.Parse("2006-01-02 15:04:05", "2021-10-01 01:02:03") // 2021-10-01 01:02:03 +0000 UTC
	if t.Add(time.Duration(2*24) * time.Hour).Before(time.Now()) {
		fmt.Println(t.Add(time.Duration(2*24) * time.Hour)) // 2021-10-03 01:02:03 +0000 UTC
		fmt.Println(time.Now())                             // 2022-02-04 17:46:15.4959484 +0800 CST m=+0.116064301
	}

}

如果文章对您有帮助,点击下方的广告,支持一下作者吧!

转载必须注明出处:

Go time包时间的常用方法 —— code.cent123.com

相关推荐


linux 系统最简单的安装 go 开发环境步骤

1. 官网下载放在 /godev 目录https://golang.google.cn/dl/或wget https://dl.google.com/go/go1.22.2.linux-amd64.tar.gz或curl -O https://dl.google.com/go/go1.22.2.linux-386.tar.gz2. 解压tar -xvf go1.22.2.linux-amd64.t

go 生成密码 php的 password_hash go实现

packagemain import( "fmt" "golang.org/x/crypto/bcrypt" ) funcmain(){ hashedPassword:=passwordHash("123456") fmt.Println(hashedPassword) hashedPassword=

使用 govcl 开发桌面UI 在 Lazarus IDE 编译中报错

使用 govcl 开发桌面UI 在 Lazarus IDE 编译中报错go build -i -buildmode=exe -ldflags="-H windowsgui" -tags="tempdll" -o "project1.exe"flag provided but not defined: -iusage: go build [

php 获取毫秒时间戳

/** *获取毫秒数 *@returnfloat */ functionmsectime() { list($msec,$sec)=explode('',microtime()); return(float)sprintf('%.0f',(floatval($msec)+floatval($sec))*1000); } print_r(msec