dongyigua4468 2016-03-03 11:57
浏览 732
已采纳

如何在Golang中将时间设置为dd-MMM-yyyy HH:mm:ss?

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.

  • 写回答

2条回答 默认 最新

  • dongzhou5344 2016-03-03 12:04
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog