2016-03-03 11:57
浏览 328

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

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.

图片转代码服务由CSDN问答提供 功能建议

导入“ fmt” 
func  main(){
 fmt.Println(time.Now()。Format(“ 01-JAN-2006 15:04:00”))
 <  p>输出应该类似于今天的日期时间是 2016-03-03 08:00:00 + 0000UTC  
输出: 03-MAR-2016 08:00:00
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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


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

    打赏 评论
  • duanba5777 2016-03-03 17:54
    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

    打赏 评论

相关推荐 更多相似问题