time.Time.ISOWeek() Function in Golang with Examples

In Go language, time packages supplies functionality for determining as well as viewing time. The ISOWeek() function in Go language is used to find the ISO 8601 year and week number in which the stated “t” happened. Where the range of the week is from 1 to 53. Moreover, this function is defined under the time package. Here, you need to import the “time” package in order to use these functions.

Syntax:

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

Here, “t” is the stated time, “year” and “week” are the two values that are returned as output in this method.

Note: Jan 01 to Jan 03 of any year say ‘n’ will most probably belong to the week 52 or 53 of year ‘n-1’ and Dec 29 to Dec 31 might be a part of the week 1 of year ‘n+1’.

Return value: It returns the ISO 8601 year and week number in which the stated “t” happened.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


Output:

year: 2013
week: 15

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
}

chevron_right


Output:

year: 2024
week: 49

Here, the “t” stated in the above code has values that are outside usual range but they are normalized while conversion.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.