📜  Golang 中的 time.Time.YearDay()函数示例

📅  最后修改于: 2021-10-25 02:57:56             🧑  作者: Mango

在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的Time.YearDay()函数用于查找“t”提供的一年中的哪一天。其中,非闰年的范围为 [1, 365],闰年的范围为 [1, 366]。而且,这个函数是在time包下定义的。在这里,您需要导入“time”包才能使用这些功能。

示例 1:

func (t Time) YearDay() int

输出:

// Golang program to illustrate the usage of
// Time.YearDay() 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 YearDay method
    yrday := t.YearDay()
  
    // Prints the day 
    // of the year as specified
    fmt.Printf("The day of the year "+
      "in the 't' specified is: %v\n", yrday)
}

示例 2:

The day of the year in the 't' specified is: 156

输出:

// Golang program to illustrate the usage of
// Time.YearDay() 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(2020, 35, 60, 
       45, 67, 94, 7567, time.UTC)
  
    // Calling YearDay method
    yrday := t.YearDay()
  
    // Prints the day of the year as specified
    fmt.Printf("The day of the year in "+
      "the 't' specified is: %v\n", yrday)
}

此处,上述“t”具有超出通常范围的值,但它们在转换时被归一化。