当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Golang time.Time.Year()用法及代码示例


在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”的值超出通常范围,但在转换时已将其标准化。




相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 time.Time.Year() Function in Golang with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。