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


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


在Go语言中,时间包提供了确定和查看时间的函数。 Go语言中的ISOWeek()函数用于查找发生所述“t”的ISO 8601年和周号。星期的范围是1到53。此外,此函数在时间包下定义。在这里,您需要导入“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”的值在通常范围之外,但在转换时已标准化。




相关用法


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