Go - time.RFC3339 时间格式化


本文摘自网络,作者,侵删。


在开发过程中,我们有时会遇到这样的问题,将2020-11-08T08:18:46+08:00转成2020-11-08 08:18:46,怎么解决这个问题?

解决这个问题,最好不要用字符串截取,或者说字符串截取是最笨的方法,这应该是时间格式化的问题。

我们先看一下 golang time 包中支持的 format 格式:

const (
 ANSIC       = "Mon Jan _2 15:04:05 2006"
 UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
 RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
 RFC822      = "02 Jan 06 15:04 MST"
 RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
 RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
 RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
 RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
 RFC3339     = "2006-01-02T15:04:05Z07:00"
 RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
 Kitchen     = "3:04PM"
 // Handy time stamps.
 Stamp      = "Jan _2 15:04:05"
 StampMilli = "Jan _2 15:04:05.000"
 StampMicro = "Jan _2 15:04:05.000000"
 StampNano  = "Jan _2 15:04:05.000000000"
)

我们找到了RFC3339,那就很简单了,我们封装一个方法RFC3339ToCSTLayout,见下面代码。

package timeutil

import "time"

var (
 cst *time.Location
)

// CSTLayout China Standard Time Layout
const CSTLayout = "2006-01-02 15:04:05"

func init() {
 var err error
 if cst, err = time.LoadLocation("Asia/Shanghai"); err != nil {
  panic(err)
 }
}

// RFC3339ToCSTLayout convert rfc3339 value to china standard time layout
func RFC3339ToCSTLayout(value string) (string, error) {
 ts, err := time.Parse(time.RFC3339, value)
 if err != nil {
  return "", err
 }

 return ts.In(cst).Format(CSTLayout), nil
}

运行一下

RFC3339Str := "2020-11-08T08:18:46+08:00"
cst, err := timeutil.RFC3339ToCSTLayout(RFC3339Str)
if err != nil {
 fmt.Println(err)
}
fmt.Println(cst)

输出:

2020-11-08 08:18:46

小结

同理,若遇到RFC3339Nano、RFC822、RFC1123等格式,也可以使用类似的方法,只需要在time.Parse()中指定时间格式即可。





本文来自:51CTO博客

感谢作者:wx6087c7391d3cd

查看原文:Go - time.RFC3339 时间格式化


相关阅读 >>

最新消息!Go 1.17 正式发布啦!

教你使用Golang和lua实现一个值班机器人

我是如何把5万行c++代码移植到Go的?

聊聊dubbo-Go-proxy的parammapper

Go 函数选项模式

一周 Go world 新鲜事

Go语言————3、编辑器、集成开发环境与其它工具

Golang中cat输出乱码问题解决方法

Go compiler intrinsics(文末赠书)

Golang怎么编写php扩展

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...