加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang时间格式化

发布时间:2020-12-16 18:12:09 所属栏目:大数据 来源:网络整理
导读:golang时间处理 相关包 "time" 当前时间戳 fmt.Println(time.Now().Unix())# 1389058332 当前格式化时间 fmt.Println(time.Now().Format("2006-01-02 15:04:05")) # 这是个奇葩,必须是这个时间点,据说是go诞生之日,记忆方法:6-1-2-3-4-5# 2014-01-07 09:42:2

golang时间处理
相关包 "time"

当前时间戳


fmt.Println(time.Now().Unix())
# 1389058332
当前格式化时间
fmt.Println(time.Now().Format("2006-01-02 15:04:05")) # 这是个奇葩,必须是这个时间点,据说是go诞生之日,记忆方法:6-1-2-3-4-5
# 2014-01-07 09:42:20
时间戳转str格式化时间


str_time:=time.Unix(1389058332,0).Format("2006-01-0215:04:05")
fmt.Println(str_time)
#2014-01-0709:32:12
str格式化时间转时间戳

the_time:=time.Date(2014,1,7,5,50,4,time.Local)
unix_time:=the_time.Unix()
fmt.Println(unix_time)
#389045004
还有一种方法,使用time.Parse


the_time,err:=time.Parse("2006-01-0215:04:05","2014-01-0809:04:41")
iferr==nil{
unix_time:=the_time.Unix()
	fmt.Println(unix_time)		
}
#1389171881



示例

package main

import (
	"fmt"
	"time"
)

const (
	date        = "2006-01-02"
	shortdate   = "06-01-02"
	times       = "15:04:02"
	shorttime   = "15:04"
	datetime    = "2006-01-02 15:04:02"
	newdatetime = "2006/01/02 15~04~02"
	newtime     = "15~04~02"
)

func main() {
	thisdate := "2014-03-17 14:55:06"
	timeformatdate,_ := time.Parse(datetime,thisdate)
	fmt.Println(timeformatdate)
	convdate := timeformatdate.Format(date)
	convshortdate := timeformatdate.Format(shortdate)
	convtime := timeformatdate.Format(times)
	convshorttime := timeformatdate.Format(shorttime)
	convnewdatetime := timeformatdate.Format(newdatetime)
	convnewtime := timeformatdate.Format(newtime)
	fmt.Println(convdate)
	fmt.Println(convshortdate)
	fmt.Println(convtime)
	fmt.Println(convshorttime)
	fmt.Println(convnewdatetime)
	fmt.Println(convnewtime)
}



格式化当前时间
lasttime,_ := time.Now().Format("2006-01-02 15:04:05")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读