英文:
What does 20060102150405 mean?
问题
我想获取当前的时间值。我找到了这个答案,它对我有用,但我不知道为什么格式化方法要使用20060102150405
这个值,而不是yyyyMMdd hhmmss
。
英文:
I would like to get current time value. I found this answer which works for me but don't know why format method take 20060102150405
value? Not like yyyyMMdd hhmmss
.
答案1
得分: 12
Go语言的时间格式化与其他语言不同而独特。Go语言不使用传统的日期格式来打印日期,而是使用参考日期20060102150405
,这看起来毫无意义,但实际上有其原因,因为它对应于Posix date
命令中的1 2 3 4 5 6
:
Mon Jan 2 15:04:05 -0700 MST 2006
0 1 2 3 4 5 6
时区是7
,但它位于中间,所以最终的格式类似于1 2 3 4 5 7 6
。
如果你正在从strftime
格式过渡,可以使用这个在线转换工具。
有趣的历史参考:https://github.com/golang/go/issues/444
time
包还提供了一些方便的常量:
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // 带数字时区的RFC822
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // 带数字时区的RFC1123
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// 方便的时间戳。
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
你可以像这样使用它们:
t := time.Now()
fmt.Println(t.Format(time.ANSIC))
英文:
Go's time formatting unique and different than what you would do in other languages. Instead of having a conventional format to print the date, Go uses the reference date 20060102150405
which seems meaningless but actually has a reason, as it's 1 2 3 4 5 6
in the Posix date
command:
Mon Jan 2 15:04:05 -0700 MST 2006
0 1 2 3 4 5 6
The timezone is 7
but that sits in the middle, so in the end the format resembles 1 2 3 4 5 7 6
.
This online converter is handy, if you are transitioning from the strftime
format.
Interesting historical reference: https://github.com/golang/go/issues/444
The time
package provides handy constants as well:
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
You can use them like this:
t := time.Now()
fmt.Println(t.Format(time.ANSIC))
答案2
得分: 4
请问您需要将以下内容翻译成中文吗?
"See https://golang.org/pkg/time/#pkg-constants It is the time "01/02 03:04:05PM '06 -0700" Because each component has a different number (1, 2, 3, etc.), it can determine from the numbers what components you want."
英文:
See https://golang.org/pkg/time/#pkg-constants It is the time "01/02 03:04:05PM '06 -0700" Because each component has a different number (1, 2, 3, etc.), it can determine from the numbers what components you want.
答案3
得分: 1
20060102150405 是一个日期和时间格式,对应的是 2006/01/02 15:04:05
包 main
import (
"fmt"
"time"
)
func main() {
date1 := time.Now().Format("2006/01/02 15:04")
fmt.Println(date1)//2009/11/10 23:00
date2 := time.Now().Format("20060102150405")
fmt.Println(date2)//20091110230000
}
https://play.golang.org/p/kIfNRQ50JP
英文:
20060102150405 is a date and time format 2006/01/02 15:04:05
package main
import (
"fmt"
"time"
)
func main() {
date1 := time.Now().Format("2006/01/02 15:04")
fmt.Println(date1)//2009/11/10 23:00
date2 := time.Now().Format("20060102150405")
fmt.Println(date2)//20091110230000
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论