在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的Time.Year()函数用于查找指定“t”发生的年份。而且,这个函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些功能。
Syntax:
Here, “t” is the stated time.
Return value: It returns the year in which the specified “t” occurs.
示例 1:
func (t Time) Year() int
输出:
// Golang program to illustrate the usage of
// Time.Year() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Declaring t in UTC
t := time.Date(2019, 6, 5, 11,
35, 04, 0, time.UTC)
// Calling Year method
yr := t.Year()
// Prints year as specified
fmt.Printf("The stated year in the"+
" 't' specified is: %v\n", yr)
}
示例 2:
The stated year in the 't' specified is: 2019
输出:
// Golang program to illustrate the usage of
// Time.Year() function
// Including main package
package main
// Importing fmt and time
import "fmt"
import "time"
// Calling main
func main() {
// Declaring t in UTC
t := time.Date(2019, 13, 35,
28, 89, 63, 0, time.UTC)
// Calling Year method
yr := t.Year()
// Prints year as specified
fmt.Printf("The stated year in the"+
" 't' specified is: %v\n", yr)
}
此处,上述“t”具有超出通常范围的值,但它们在转换时被归一化。