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

📅  最后修改于: 2021-10-24 14:06:14             🧑  作者: Mango

在 Go 语言中,时间包提供了确定和查看时间的功能。 Go 语言中的ISOWeek()函数用于查找 ISO 8601 中规定的“t”发生的年份和周数。其中一周的范围是从 1 到 53。而且,这个函数是在 time 包下定义的。在这里,您需要导入“time”包才能使用这些功能。

句法:

func (t Time) ISOWeek() (year, week int)

这里,“t”是指定的时间,“year”和“week”是此方法中作为输出返回的两个值。

注意:任何一年的 1 月 1 日至 1 月 3 日说“n”很可能属于“n-1”年的第 52 或 53 周,而 12 月 29 日至 12 月 31 日可能是“n+”年第 1 周的一部分1’。

返回值:它返回 ISO 8601 中规定的“t”发生的年和周数。

示例 1:

// Golang program to illustrate the usage of
// Time.ISOWeek() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t for ISOWeek method
    t := time.Date(2013, 4, 11, 12, 37, 33, 0, time.UTC)
  
    // Calling ISOWeek() method
    year, week := t.ISOWeek()
  
    // Prints the year
    fmt.Printf("year: %v\n", year)
  
    // Prints the week number
    fmt.Printf("week: %d\n", week)
}

输出:

year: 2013
week: 15

示例 2:

// Golang program to illustrate the usage of
// Time.ISOWeek() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Defining t for ISOWeek method
    t := time.Date(2022, 35, 37, 29, 99, 70, 6388, time.UTC)
  
    // Calling ISOWeek() method
    year, week := t.ISOWeek()
  
    // Prints the year
    fmt.Printf("year: %v\n", year)
  
    // Prints the week number
    fmt.Printf("week: %d\n", week)
}

输出:

year: 2024
week: 49

这里,上述代码中的“t”值超出了通常的范围,但它们在转换时被归一化。