使用”yyyyMMddHHmmss”格式化当前时间的方法是:

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

How to format current time using a yyyyMMddHHmmss format?

问题

我正在尝试使用格式yyyyMMddHHmmss来格式化当前时间。

t := time.Now()
fmt.Println(t.Format("yyyyMMddHHmmss"))

输出结果为:

yyyyMMddHHmmss

有什么建议吗?

英文:

I'm trying to format the current time using this format yyyyMMddHHmmss.

t := time.Now()
fmt.Println(t.Format("yyyyMMddHHmmss"))

That is outputting:

yyyyMMddHHmmss

Any suggestions?

答案1

得分: 394

使用以下代码将日期格式化为指定的格式:

fmt.Println(t.Format("20060102150405"))

Go语言使用以下常量来格式化日期,请参考这里

const (
    stdLongMonth      = "January"
    stdMonth          = "Jan"
    stdNumMonth       = "1"
    stdZeroMonth      = "01"
    stdLongWeekDay    = "Monday"
    stdWeekDay        = "Mon"
    stdDay            = "2"
    stdUnderDay       = "_2"
    stdZeroDay        = "02"
    stdHour           = "15"
    stdHour12         = "3"
    stdZeroHour12     = "03"
    stdMinute         = "4"
    stdZeroMinute     = "04"
    stdSecond         = "5"
    stdZeroSecond     = "05"
    stdLongYear       = "2006"
    stdYear           = "06"
    stdPM             = "PM"
    stdpm             = "pm"
    stdTZ             = "MST"
    stdISO8601TZ      = "Z0700"  // 打印Z表示UTC
    stdISO8601ColonTZ = "Z07:00" // 打印Z表示UTC
    stdNumTZ          = "-0700"  // 总是数字
    stdNumShortTZ     = "-07"    // 总是数字
    stdNumColonTZ     = "-07:00" // 总是数字
    stdFracSecond0    = ".0", ".00" // 包括尾随的零
    stdFracSecond9    = ".9", ".99" // 省略尾随的零
)
英文:

Use

fmt.Println(t.Format("20060102150405"))

as Go uses following constants to format date,refer here

const (
    stdLongMonth      = "January"
    stdMonth          = "Jan"
    stdNumMonth       = "1"
    stdZeroMonth      = "01"
    stdLongWeekDay    = "Monday"
    stdWeekDay        = "Mon"
    stdDay            = "2"
    stdUnderDay       = "_2"
    stdZeroDay        = "02"
    stdHour           = "15"
    stdHour12         = "3"
    stdZeroHour12     = "03"
    stdMinute         = "4"
    stdZeroMinute     = "04"
    stdSecond         = "5"
    stdZeroSecond     = "05"
    stdLongYear       = "2006"
    stdYear           = "06"
    stdPM             = "PM"
    stdpm             = "pm"
    stdTZ             = "MST"
    stdISO8601TZ      = "Z0700"  // prints Z for UTC
    stdISO8601ColonTZ = "Z07:00" // prints Z for UTC
    stdNumTZ          = "-0700"  // always numeric
    stdNumShortTZ     = "-07"    // always numeric
    stdNumColonTZ     = "-07:00" // always numeric
    stdFracSecond0    = ".0", ".00" // trailing zeros included
    stdFracSecond9    = ".9", ".99" // trailing zeros omitted
)

答案2

得分: 44

当你在Google搜索"golang当前时间格式"时,这个问题会排在前面,所以对于所有想要使用其他格式的人来说,记住你可以随时调用以下方法:

t := time.Now()

t.Year()

t.Month()

t.Day()

t.Hour()

t.Minute()

t.Second()

例如,要获取当前日期时间为"YYYY-MM-DDTHH:MM:SS"(例如2019-01-22T12:40:55),你可以使用以下方法和fmt.Sprintf:

t := time.Now()
formatted := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
        t.Year(), t.Month(), t.Day(),
        t.Hour(), t.Minute(), t.Second())

如常,记住文档是学习的最佳来源:https://golang.org/pkg/time/

英文:

This question comes in top of Google search when you find "golang current time format" so, for all the people that want to use another format, remember that you can always call to:

t := time.Now()

t.Year()

t.Month()

t.Day()

t.Hour()

t.Minute()

t.Second()

For example, to get current date time as "YYYY-MM-DDTHH:MM:SS" (for example 2019-01-22T12:40:55) you can use these methods with fmt.Sprintf:

t := time.Now()
formatted := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
        t.Year(), t.Month(), t.Day(),
        t.Hour(), t.Minute(), t.Second())

As always, remember that docs are the best source of learning: https://golang.org/pkg/time/

答案3

得分: 26

选项1:Go标准库

t.Format("20060102150405")
单位 Golang布局 示例 备注
06 21, 81, 01
2006 2021, 1981, 0001
January January, February, December
Jan Jan, Feb, Dec
1 1, 2, 12
01 01, 02, 12
Monday Monday, Wednesday, Sunday
Mon Mon, Wed, Sun
2 1, 2, 11, 31
02 01, 02, 11, 31 月份前补零
_2 ⎵1, ⎵2, 11, 31 月份前补空格
002 001, 002, 011, 031, 145, 365, 366 年份前补零
__2 ⎵⎵1, ⎵⎵2, ⎵11, ⎵31, 365, 366 年份前补空格
时间段 PM AM, PM
时间段 pm am, pm
24小时制 15 00, 01, 12, 23
12小时制 3 1, 2, 12
12小时制 03 01, 02, 12
分钟 4 0, 4 ,10, 35
分钟 04 00, 04 ,10, 35
5 0, 5, 25
05 00, 05, 25
10的-1次方到10的-9次方秒 .0 .000000000 .1, .199000000 包含尾随零
10的-1次方到10的-9次方秒 .9 .999999999 .1, .199 省略尾随零
时区 MST UTC, MST, CET
时区 Z07 Z, +08, -05 Z表示UTC
时区 Z0700 Z, +0800, -0500 Z表示UTC
时区 Z070000 Z, +080000, -050000 Z表示UTC
时区 Z07:00 Z, +08:00, -05:00 Z表示UTC
时区 Z07:00:00 Z, +08:00:00, -05:00:00 Z表示UTC
时区 -07 +00, +08, -05
时区 -0700 +0000, +0800, -0500
时区 -070000 +000000, +080000, -050000
时区 -07:00 +00:00, +08:00, -05:00
时区 -07:00:00 +00:00:00, +08:00:00, -05:00:00

在Golang 1.17+中,对于秒的小数部分(.999或.000),可以使用,代替.(,999或,000),但输出始终带有.!请参阅https://github.com/golang/go/issues/48037

选项2:strftime Go实现

import strftime "github.com/itchyny/timefmt-go"
strftime.Format(t, "%Y%m%d%H%M%S")

更多信息请参见:

https://github.com/itchyny/timefmt-go

https://linux.die.net/man/3/strftime

英文:

Option 1: Go standard library

t.Format("20060102150405")
Unit Golang Layout Examples Note
Year 06 21, 81, 01
Year 2006 2021, 1981, 0001
Month January January, February, December
Month Jan Jan, Feb, Dec
Month 1 1, 2, 12
Month 01 01, 02, 12
Day Monday Monday, Wednesday, Sunday
Day Mon Mon, Wed, Sun
Day 2 1, 2, 11, 31
Day 02 01, 02, 11, 31 zero padded day of the month
Day _2 ⎵1, ⎵2, 11, 31 space padded day of the month
Day 002 001, 002, 011, 031, 145, 365, 366 zero padded day of the year
Day __2 ⎵⎵1, ⎵⎵2, ⎵11, ⎵31, 365, 366 space padded day of the year
Part of day PM AM, PM
Part of day pm am, pm
Hour 24h 15 00, 01, 12, 23
Hour 12h 3 1, 2, 12
Hour 12h 03 01, 02, 12
Minute 4 0, 4 ,10, 35
Minute 04 00, 04 ,10, 35
Second 5 0, 5, 25
Second 05 00, 05, 25
10<sup>-1</sup> to 10<sup>-9</sup> s .0 .000000000 .1, .199000000 Trailing zeros included
10<sup>-1</sup> to 10<sup>-9</sup> s .9 .999999999 .1, .199 Trailing zeros omitted
Time zone MST UTC, MST, CET
Time zone Z07 Z, +08, -05 Z is for UTC
Time zone Z0700 Z, +0800, -0500 Z is for UTC
Time zone Z070000 Z, +080000, -050000 Z is for UTC
Time zone Z07:00 Z, +08:00, -05:00 Z is for UTC
Time zone Z07:00:00 Z, +08:00:00, -05:00:00 Z is for UTC
Time zone -07 +00, +08, -05
Time zone -0700 +0000, +0800, -0500
Time zone -070000 +000000, +080000, -050000
Time zone -07:00 +00:00, +08:00, -05:00
Time zone -07:00:00 +00:00:00, +08:00:00, -05:00:00

In Golang 1.17+ for fraction of seconds (.999 or .000) you can use , instead of . (,999 or ,000) but output is always with .!!! See https://github.com/golang/go/issues/48037

Option 2: strftime Go implementation

import strftime &quot;github.com/itchyny/timefmt-go&quot;
strftime.Format(t, &quot;%Y%m%d%H%M%S&quot;)

See for more info

https://github.com/itchyny/timefmt-go

https://linux.die.net/man/3/strftime

答案4

得分: 13

import "time"

layout := "2006-01-02T15:04:05.000Z"
str := "2014-11-12T11:45:26.371Z"
t, err := time.Parse(layout, str)
if err != nil {
    fmt.Println(err)
}
fmt.Println(t)

输出结果为:

>> 2014-11-12 11:45:26.371 +0000 UTC
英文:
import(&quot;time&quot;)

layout := &quot;2006-01-02T15:04:05.000Z&quot;
str := &quot;2014-11-12T11:45:26.371Z&quot;
t, err := time.Parse(layout, str)
if err != nil {
    fmt.Println(err)
}
fmt.Println(t)

gives:

&gt;&gt; 2014-11-12 11:45:26.371 +0000 UTC

答案5

得分: 8

Golang中的Time包有一些值得一看的方法。

func (Time) Format

func (t Time) Format(layout string) string

Format方法根据layout定义的格式返回时间值的文本表示。layout通过展示参考时间的格式来定义输出的格式。

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

这个参考时间的展示方式将被应用到时间值上。预定义的layout如ANSIC、UnixDate、RFC3339等描述了参考时间的标准和方便的表示方式。有关格式和参考时间定义的更多信息,请参阅ANSIC和此包定义的其他常量的文档。

来源(http://golang.org/pkg/time/#Time.Format)

我还找到了一个定义layout的示例(http://golang.org/src/pkg/time/example_test.go)

func ExampleTime_Format() {
    // layout shows by example how the reference time should be represented.
    const layout = "Jan 2, 2006 at 3:04pm (MST)"
    t := time.Date(2009, time.November, 10, 15, 0, 0, 0, time.Local)
    fmt.Println(t.Format(layout))
    fmt.Println(t.UTC().Format(layout))
    // Output:
    // Nov 10, 2009 at 3:00pm (PST)
    // Nov 10, 2009 at 11:00pm (UTC)
}

以上是要翻译的内容。

英文:

Time package in Golang has some methods that might be worth looking.

>func (Time) Format

>func (t Time) Format(layout string) string
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
would be displayed if it were the value; it serves as an example of the desired output. The same display rules will then be applied to the time value. Predefined layouts ANSIC, UnixDate, RFC3339 and others describe standard and convenient representations of the reference time. For more information about the formats and the definition of the reference time, see the documentation for ANSIC and the other constants defined by this package.

Source (http://golang.org/pkg/time/#Time.Format)

I also found an example of defining the layout (http://golang.org/src/pkg/time/example_test.go)

func ExampleTime_Format() {
		// layout shows by example how the reference time should be represented.
		const layout = &quot;Jan 2, 2006 at 3:04pm (MST)&quot;
		t := time.Date(2009, time.November, 10, 15, 0, 0, 0, time.Local)
		fmt.Println(t.Format(layout))
		fmt.Println(t.UTC().Format(layout))
		// Output:
   	// Nov 10, 2009 at 3:00pm (PST)
		// Nov 10, 2009 at 11:00pm (UTC)
    }

答案6

得分: 7

Go标准库:time

now := time.Now()
fmt.Println(now) // 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
fmt.Println(now.Format("20060102150405"))
fmt.Println(now.Format("2006/01/02/15/04/05"))
fmt.Println(now.Format("2006-01-02 15:04:05"))
fmt.Println(now.Format("2006-01-02 15:04"))
fmt.Println(now.Format("2006/01/02 15:04:05"))
fmt.Println(now.Format("2006/01/02 15:04:05 (-0700)"))
fmt.Println(now.Format("2006年01月02日 15:04"))
fmt.Println(now.Format(time.Layout))   // 01/02 03:04:05PM '06 -0700
fmt.Println(now.Format(time.ANSIC))    // Mon Jan _2 15:04:05 2006
fmt.Println(now.Format(time.UnixDate)) // Mon Jan _2 15:04:05 MST 2006
fmt.Println(now.Format(time.RubyDate)) // Mon Jan 02 15:04:05 -0700 2006
fmt.Println(now.Format(time.RFC822))   // 02 Jan 06 15:04 MST
fmt.Println(now.Format(time.RFC850))   // Monday, 02-Jan-06 15:04:05 MST
fmt.Println(now.Format(time.Kitchen))  // 3:04PM
fmt.Println(now.Format(time.Stamp))    // Jan _2 15:04:05

<kbd>Go playground</kbd>

英文:

Go standard library: time

now := time.Now()
fmt.Println(now) // 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
fmt.Println(now.Format(&quot;20060102150405&quot;))
fmt.Println(now.Format(&quot;2006/01/02/15/04/05&quot;))
fmt.Println(now.Format(&quot;2006-01-02 15:04:05&quot;))
fmt.Println(now.Format(&quot;2006-01-02 15:04&quot;))
fmt.Println(now.Format(&quot;2006/01/02 15:04:05&quot;))
fmt.Println(now.Format(&quot;2006/01/02 15:04:05 (-0700)&quot;))
fmt.Println(now.Format(&quot;2006年01月02日 15:04&quot;))
fmt.Println(now.Format(time.Layout))   // 01/02 03:04:05PM &#39;06 -0700
fmt.Println(now.Format(time.ANSIC))    // Mon Jan _2 15:04:05 2006
fmt.Println(now.Format(time.UnixDate)) // Mon Jan _2 15:04:05 MST 2006
fmt.Println(now.Format(time.RubyDate)) // Mon Jan 02 15:04:05 -0700 2006
fmt.Println(now.Format(time.RFC822))   // 02 Jan 06 15:04 MST
fmt.Println(now.Format(time.RFC850))   // Monday, 02-Jan-06 15:04:05 MST
fmt.Println(now.Format(time.Kitchen))  // 3:04PM
fmt.Println(now.Format(time.Stamp))    // Jan _2 15:04:05

<kbd>Go playground</kbd>

huangapple
  • 本文由 发表于 2013年11月27日 12:45:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/20234104.html
匿名

发表评论

匿名网友

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

确定