ser*_*erg 98 format time timestamp go
我正在尝试使用此格式格式化当前时间yyyyMMddHHmmss.
t := time.Now()
fmt.Println(t.Format("yyyyMMddHHmmss"))
Run Code Online (Sandbox Code Playgroud)
那是输出:
yyyyMMddHHmmss
Run Code Online (Sandbox Code Playgroud)
有什么建议?
Cri*_*ris 198
使用
fmt.Println(t.Format("20060102150405"))
Run Code Online (Sandbox Code Playgroud)
由于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
)
Run Code Online (Sandbox Code Playgroud)
Men*_*nyT 28
t.Format("20060102150405")\nRun Code Online (Sandbox Code Playgroud)\n| 单元 | Go语言布局 | 例子 | 笔记 |
|---|---|---|---|
| 年 | 06 | 21, 81, 01 | |
| 年 | 2006年 | 2021, 1981, 0001 | |
| 月 | 一月 | 一月、二月、十二月 | |
| 月 | 扬 | 一月、二月、十二月 | |
| 月 | 1 | 1, 2, 12 | |
| 月 | 01 | 01, 02, 12 | |
| 天 | 周一 | 周一、周三、周日 | |
| 天 | 周一 | 周一、周三、周日 | |
| 天 | 2 | 1, 2, 11, 31 | |
| 天 | 02 | 01, 02, 11, 31 | 月份中用零填充的日期 |
| 天 | _2 | \xe2\x8e\xb51, \xe2\x8e\xb52, 11, 31 | 一个月中的某天用空格填充 |
| 天 | 002 | 001、002、011、031、145、365、366 | 一年中的零填充日 |
| 天 | __2 | \xe2\x8e\xb5\xe2\x8e\xb51、\xe2\x8e\xb5\xe2\x8e\xb52、\xe2\x8e\xb511、\xe2\x8e\xb531、365、366 | 一年中的空格填充日 |
| 一天的一部分 | 下午 | 上午下午 | |
| 一天的一部分 | 下午 | 上午下午 | |
| 小时 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
import strftime "github.com/itchyny/timefmt-go"\nRun Code Online (Sandbox Code Playgroud)\nstrftime.Format(t, "%Y%m%d%H%M%S")\nRun Code Online (Sandbox Code Playgroud)\n查看更多信息
\nhttps://github.com/itchyny/timefmt-go
\nhttps://linux.die.net/man/3/strftime
\nLui*_*ito 13
当您找到“ golang当前时间格式”时,此问题会出现在Google搜索的顶部,因此,对于所有希望使用其他格式的人,请记住,您可以随时致电:
t := time.Now()
t.Year()
t.Month()
t.Day()
t.Hour()
t.Minute()
t.Second()
Run Code Online (Sandbox Code Playgroud)
例如,要获取当前日期时间为“ 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())
Run Code Online (Sandbox Code Playgroud)
与往常一样,请记住文档是最好的学习资源:https://golang.org/pkg/time/
小智 9
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)
Run Code Online (Sandbox Code Playgroud)
给出:
>> 2014-11-12 11:45:26.371 +0000 UTC
Run Code Online (Sandbox Code Playgroud)
Go标准库:时间
\nnow := time.Now()\nfmt.Println(now) // 2009-11-10 23:00:00 +0000 UTC m=+0.000000001\nfmt.Println(now.Format("20060102150405"))\nfmt.Println(now.Format("2006/01/02/15/04/05"))\nfmt.Println(now.Format("2006-01-02 15:04:05"))\nfmt.Println(now.Format("2006-01-02 15:04"))\nfmt.Println(now.Format("2006/01/02 15:04:05"))\nfmt.Println(now.Format("2006/01/02 15:04:05 (-0700)"))\nfmt.Println(now.Format("2006\xe5\xb9\xb401\xe6\x9c\x8802\xe6\x97\xa5 15:04"))\nfmt.Println(now.Format(time.Layout)) // 01/02 03:04:05PM \'06 -0700\nfmt.Println(now.Format(time.ANSIC)) // Mon Jan _2 15:04:05 2006\nfmt.Println(now.Format(time.UnixDate)) // Mon Jan _2 15:04:05 MST 2006\nfmt.Println(now.Format(time.RubyDate)) // Mon Jan 02 15:04:05 -0700 2006\nfmt.Println(now.Format(time.RFC822)) // 02 Jan 06 15:04 MST\nfmt.Println(now.Format(time.RFC850)) // Monday, 02-Jan-06 15:04:05 MST\nfmt.Println(now.Format(time.Kitchen)) // 3:04PM\nfmt.Println(now.Format(time.Stamp)) // Jan _2 15:04:05\nRun Code Online (Sandbox Code Playgroud)\n\n
Golang中的Time包有一些值得一看的方法.
func(时间)格式
func(t Time)格式(布局字符串)string格式返回根据布局格式化的时间值的文本表示,通过显示参考时间如何定义格式,
Mon Jan 2 15:04:05 -0700 MST 2006如果是值,将显示; 它作为所需输出的一个例子.然后将相同的显示规则应用于时间值.预定义布局ANSIC,UnixDate,RFC3339和其他描述了参考时间的标准和方便的表示.有关格式和参考时间定义的更多信息,请参阅ANSIC的文档以及此程序包定义的其他常量.
来源(http://golang.org/pkg/time/#Time.Format)
我还找到了一个定义布局的例子(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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84788 次 |
| 最近记录: |