How do i format time in Go Language

huangapple go评论81阅读模式
英文:

How do i format time in Go Language

问题

我需要将时间格式化为以下形式:

Mon, 16 Jun 2014 09:19:01 +0200 

使用以下代码:

a := time.RFC1123Z

给我:

Mon, 02 Jan 2006 15:04:05 -0700

这个看起来是正确的,但我需要当前时间,并以某种方式使用now(),我猜想。但我还没有弄清楚如何做到。

英文:

I need to format time like this

Mon, 16 Jun 2014 09:19:01 +0200 

following code

a := time.RFC1123Z

give me

Mon, 02 Jan 2006 15:04:05 -0700

which seem correct but need current time and use now() somehow i guess. but havent figured out how.

答案1

得分: 4

你需要使用Format来实现。time.RFC1123Z只是一个布局字符串。

t := time.Now()
s := t.Format(time.RFC1123Z) // 使用给定的布局将t格式化为字符串
fmt.Println(s)
英文:

You need to use Format for this. time.RFC1123Z is just a layout string.

t := time.Now()
s := t.Format(time.RFC1123Z) // Format t to a string using the given layout
fmt.Println(s)

huangapple
  • 本文由 发表于 2014年8月2日 18:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/25094243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定