英文:
Time Layout in Golang
问题
我知道在Go语言中,我们需要使用time.Time接口来处理日期。
而要进行格式化,我们需要使用format函数。
http://golang.org/pkg/time/#Time.Format
但是,在Golang中,time.Time有哪些有效的和不同的格式化选项呢?
英文:
I know we need to use the time.Time interface for dates in Go.
And to format, we need to use the format function.
http://golang.org/pkg/time/#Time.Format
But what are the valid and different formatters available for time.Time in Golang?
答案1
得分: 4
time.Format的文档位于http://golang.org/pkg/time/#Time.Format:
预定义的布局ANSIC、UnixDate、RFC3339和其他布局描述了参考时间的标准和便捷表示。有关格式和参考时间定义的更多信息,请参阅ANSIC的文档以及此包定义的其他常量的文档。
因此,在常量http://golang.org/pkg/time/#pkg-constants中:
要定义自己的格式,请写下以您的方式格式化的参考时间;参考ANSIC、StampMicro或Kitchen等常量的值以获取示例。模型是演示参考时间的样子,以便Format和Parse方法可以将相同的转换应用于一般时间值。
简而言之:您可以按照您想要的格式编写参考时间Mon Jan 2 15:04:05 MST 2006
,并将该字符串传递给Time.Format()方法。
正如@Volker所说,请阅读文档并了解类型和接口之间的区别。
英文:
The docs for time.Format say http://golang.org/pkg/time/#Time.Format:
> 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.
So, in the constants http://golang.org/pkg/time/#pkg-constants:
> To define your own format, write down what the reference time would look like formatted your way; see the values of constants like ANSIC, StampMicro or Kitchen for examples. The model is to demonstrate what the reference time looks like so that the Format and Parse methods can apply the same transformation to a general time value.
In short: you write the reference time Mon Jan 2 15:04:05 MST 2006
in the format you want and pass that string to Time.Format()
As @Volker said, please read the docs and read about the difference between types and interfaces.
答案2
得分: 2
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format(time.Kitchen))
t = time.Now()
tf := t.Format("2006-01-02 15:04:05-07:00")
fmt.Println(tf)
}
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format(time.Kitchen))
t = time.Now()
tf := t.Format("2006-01-02 15:04:05-07:00")
fmt.Println(tf)
}
这是一个Go语言的程序,它使用了time包来处理时间。程序的功能是获取当前时间,并以不同的格式进行输出。
第一个输出使用了time.Kitchen
常量,它代表了一个简化的时间格式,类似于"3:04PM"。
第二个输出使用了自定义的时间格式"2006-01-02 15:04:05-07:00",其中的数字和符号代表了不同的时间单位,例如"2006"代表年份,"01"代表月份,"02"代表日期,"15"代表小时(24小时制),"04"代表分钟,"05"代表秒,"-07:00"代表时区偏移量。
希望对你有帮助!
英文:
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format(time.Kitchen))
t := time.Now()
tf := t.Format("2006-01-02 15:04:05-07:00")
fmt.Println(tf)
}
答案3
得分: 0
如果您需要自定义布局和/或在内置布局方面遇到困难,可以使用以下代码:
type Rtime struct {
Year int
Month int
Day int
Hour int
Minute int
Second int
Nanosecond int
Millisecond int
Offset int
OffsetString string
Zone string
}
func (rt *Rtime) LocalNow() {
t := time.Now()
rt.Hour, rt.Minute, rt.Second = t.Clock()
rt.Nanosecond = t.Nanosecond()
rt.Millisecond = rt.Nanosecond / 1000000
rt.Month = int(t.Month())
rt.Day = t.Day()
rt.Year = t.Year()
rt.OffsetString = ""
rt.Zone, rt.Offset = t.Local().Zone()
if rt.Offset > 0 {
rt.OffsetString = fmt.Sprintf("+%02d%02d",
rt.Offset/(60*60),
rt.Offset%(60*60)/60)
} else {
rt.OffsetString = fmt.Sprintf("%02d%02d",
rt.Offset/(60*60),
rt.Offset%(60*60)/60)
}
}
str := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %s %s %d",
rt.Year, rt.Month, rt.Day, rt.Hour, rt.Minute, rt.Second,
rt.Millisecond, rt.Zone, rt.OffsetString, rt.Nanosecond)
fmt.Println(str)
输出结果为:
2021-06-06 09:21:54.949 EEST +0300 949861778
英文:
In case you need custom layout and/or struggling with build in layouts.
type Rtime struct {
Year int
Month int
Day int
Hour int
Minute int
Second int
Nanosecond int
Millisecond int
Offset int
OffsetString string
Zone string
}
func (rt *Rtime) LocalNow() {
t := time.Now()
rt.Hour,rt.Minute,rt.Second = t.Clock()
rt.Nanosecond = t.Nanosecond()
rt.Millisecond = rt.Nanosecond / 1000000
rt.Month = int(t.Month())
rt.Day = t.Day()
rt.Year = t.Year()
rt.OffsetString = ""
rt.Zone, rt.Offset = t.Local().Zone()
if rt.Offset > 0 {
rt.OffsetString = fmt.Sprintf("+%02d%02d",
rt.Offset/(60*60),
rt.Offset%(60*60)/60)
} else {
rt.OffsetString = fmt.Sprintf("%02d%02d",
rt.Offset/(60*60),
rt.Offset%(60*60)/60)
}
}
str := fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d %s %s %d",
rt.Year,rt.Month,rt.Day,rt.Hour,rt.Minute,rt.Second,
rt.Millisecond,rt.Zone,rt.OffsetString,rt.Nanosecond)
fmt.Println(str)
output
2021-06-06 09:21:54.949 EEST +0300 949861778
答案4
得分: -1
Golang规定了不同的标准来获取有效的日期。
可在http://golang.org/pkg/time/#Time.Format中找到。
英文:
Golang prescribes different standards to be followed for getting valid dates.
Available in http://golang.org/pkg/time/#Time.Format
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论