當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。