Open In App

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

Last Updated : 28 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

year: 2013
week: 15

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


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads