在Go语言中,时间包提供了确定和查看时间的函数。 Go语言中的Time.Year()函数用于查找指定的“t”发生的年份。此外,此函数在时间包下定义。在这里,您需要导入“time”包才能使用这些函数。
用法:
func (t Time) Year() int
在这里,“t”是规定的时间。
返回值:它返回指定的“t”发生的年份。
范例1:
// 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)
}
输出:
The stated year in the 't' specified is:2019
范例2:
// 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)
}
输出:
The stated year in the 't' specified is:2020
此处,上述“t”的值超出通常范围,但在转换时已将其标准化。
相关用法
- Golang math.Lgamma()用法及代码示例
- Golang math.Float64bits()用法及代码示例
- Golang atomic.AddInt64()用法及代码示例
- Golang atomic.StoreInt64()用法及代码示例
- Golang reflect.FieldByIndex()用法及代码示例
- Golang string.Contains用法及代码示例
- Golang bits.Sub()用法及代码示例
- Golang io.PipeWriter.CloseWithError()用法及代码示例
- Golang time.Round()用法及代码示例
- Golang reflect.AppendSlice()用法及代码示例
- Golang reflect.ChanOf()用法及代码示例
- Golang flag.Bool()用法及代码示例
- Golang time.Sleep()用法及代码示例
- Golang reflect.DeepEqual()用法及代码示例
- Golang reflect.Indirect()用法及代码示例
- Golang reflect.CanAddr()用法及代码示例
- Golang reflect.CanInterface()用法及代码示例
- Golang reflect.CanSet()用法及代码示例
- Golang reflect.Cap()用法及代码示例
- Golang strings.ContainsRune()用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 time.Time.Year() Function in Golang with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。