How to set time to dd-MMM-yyyy HH:mm:ss in go?

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

How to set time to dd-MMM-yyyy HH:mm:ss in go?

问题

以下是翻译好的代码:

package main

import "fmt"
import "time"

func main() {
	fmt.Println(time.Now().Format("01-JAN-2006 15:04:00"))
}

如果今天的日期时间是 2016-03-03 08:00:00 +0000UTC,输出应该是 03-MAR-2016 08:00:00,时间应该是24小时制。

英文:

https://play.golang.org/p/82QgBdoI2G

package main

import "fmt"
import "time"

func main() {
	fmt.Println(time.Now().Format("01-JAN-2006 15:04:00"))
}

The output should be like if date time today is 2016-03-03 08:00:00 +0000UTC
Output: 03-MAR-2016 08:00:00
Time should be in 24hr format.

答案1

得分: 13

你的布局不正确,它应该显示参考时间在你想要的格式中的表示方式,其中参考时间是Mon Jan 2 15:04:05 -0700 MST 2006

你的布局应该是:

"02-Jan-2006 15:04:05"

注意秒部分的05。由于你指定了小时为15,这是24小时制。303是12小时制。

fmt.Println(time.Now().Format("02-Jan-2006 15:04:05"))

对我来说,它打印出:

03-Mar-2016 13:03:10

还要注意月份使用Jan,不识别JAN。如果你想要大写的月份,可以使用strings.ToUpper()

fmt.Println(strings.ToUpper(time.Now().Format("02-Mar-2006 15:04:05")))

输出:

03-MAR-2016 13:03:10

还要注意,在Go Playground上,当你的应用程序启动时,时间总是设置为一个常量(即2009-11-10 23:00:00 +0000 UTC)。

英文:

Your layout is incorrect, it should show how the reference time is represented in the format you want, where the reference time is Mon Jan 2 15:04:05 -0700 MST 2006.

Your layout should be:

"02-Jan-2006 15:04:05"

Note the 05 for the seconds part. And since you specified the hours as 15, that is 24-hour format. 3 or 03 is for the 12-hour format.

fmt.Println(time.Now().Format("02-Jan-2006 15:04:05"))

For me it prints:

03-Mar-2016 13:03:10

Also note Jan for months, JAN is not recognized. If you want uppercased month, you may use strings.ToUpper():

fmt.Println(strings.ToUpper(time.Now().Format("02-Mar-2006 15:04:05")))

Output:

03-MAR-2016 13:03:10

Also note that on the Go Playground the time is always set to a constant when your application is started (which is 2009-11-10 23:00:00 +0000 UTC).

答案2

得分: 2

fmt.Println(time.Now().Format("02-Jan-2006 15:04:05"))

参见Time包常量

布局中使用的参考时间是特定时间:

Mon Jan 2 15:04:05 MST 2006

这是Unix时间1136239445。由于MST是GMT-0700,参考时间可以被视为

01/02 03:04:05PM '06 -0700

英文:
fmt.Println(time.Now().Format("02-Jan-2006 15:04:05"))

See Time package constants

> The reference time used in the layouts is the specific time:
>
> Mon Jan 2 15:04:05 MST 2006
>
> which is Unix time 1136239445. Since MST is GMT-0700, the reference time can be thought of as
>
> 01/02 03:04:05PM '06 -0700

huangapple
  • 本文由 发表于 2016年3月3日 19:57:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/35771588.html
匿名

发表评论

匿名网友

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

确定