我正在尝试使用这种格式yyyyMMddHHmmss格式化当前时间。
t := time.Now()
fmt.Println(t.Format("yyyyMMddHHmmss"))
这就是输出:
yyyyMMddHHmmss
有什么建议吗?
我正在尝试使用这种格式yyyyMMddHHmmss格式化当前时间。
t := time.Now()
fmt.Println(t.Format("yyyyMMddHHmmss"))
这就是输出:
yyyyMMddHHmmss
有什么建议吗?
当前回答
去标准库:时间
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
去操场
其他回答
选项1:使用标准库
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-1 to 10-9 s | .0 .000000000 | .1, .199000000 | Trailing zeros included |
10-1 to 10-9 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 |
在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
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
去标准库:时间
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
去操场
Use
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" // 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
)
当你找到“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/