如何将时间设置为 dd-MMM-yyyy HH:mm:ss in go? [英] How to set time to dd-MMM-yyyy HH:mm:ss in go?

查看:62
本文介绍了如何将时间设置为 dd-MMM-yyyy HH:mm:ss in go?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

包主导入fmt"导入时间"功能主(){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 小时制.

解决方案

您的布局不正确,它应该以您想要的格式显示参考时间,其中参考时间是 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).>

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.

解决方案

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).

这篇关于如何将时间设置为 dd-MMM-yyyy HH:mm:ss in go?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆