在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的Time.String()函数用于生成借助格式字符串格式化的时间。而且,这个函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些功能。
Syntax:
Here, “t” is the stated time.
Note: It the stated time has a clock reading that is monotonic then the string that is returned as an output includes a final field i.e, “m=±value”. Where value is the unvaried clock reading that is arranged as a decimal number of seconds.
Return Value: It returns the time which is formatted with the help of format string.
示例 1:
func (t Time) String() string
输出:
// Golang program to illustrate the usage of
// Time.String() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Calling main
func main() {
// Defining the time for String method
Time := time.Date(2020, 11, 14, 10, 45, 16, 0, time.UTC)
// Calling String method
t := Time.String()
// Prints output
fmt.Printf("The time without nanoseconds is: %v\n", t)
}
示例 2:
The time without nanoseconds is: 2020-11-14 10:45:16 +0000 UTC
输出:
// Golang program to illustrate the usage of
// Time.String() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Calling main
func main() {
// Defining the time for String method
Time := time.Date(2020, 11, 14, 36, 45, 16, 36, time.UTC)
// Calling String method
t := Time.String()
// Prints output
fmt.Printf("The time with nanoseconds is: %v\n", t)
}
此处,上述代码中所述的小时超出了通常的范围,但已标准化,同时转换和纳秒也包含在上述时间中。