dongye6377 2014-11-29 21:16
浏览 37
已采纳

日期格式显示错误的日期

I'm trying to format a date like this: [daynumber] [monthname] [fullyear]

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()
    fmt.Println(t.Format("1 January 2014"))
}

However this prints out "11 November 10110" instead of the correct date "29 November 2014".

What is the correct way to use Time.Format?

  • 写回答

1条回答 默认 最新

  • douzhuo6270 2014-11-29 21:21
    关注

    Try:

    fmt.Println(t.Format("2 January 2006"))
    

    From Time.Format()

    Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time,

    Mon Jan 2 15:04:05 -0700 MST 2006
    

    The article "Parsing and formatting date/time in Go " adds:

    The use of a mnemonic over obscure formatting codes I think reflects the pragmatism of Go’s developers and their focus on creating a language that makes its users more productive

    Ironically, I have trouble remembering the exact values and order of that format template.
    (Especially the day and month that I keep mixing up, being used to the dd-mm convention, as opposed to mm-dd).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见