Related Articles

Related Articles

time.Time.YearDay() Function in Golang with Examples
  • Last Updated : 21 Apr, 2020

In Go language, time packages supplies functionality for determining as well as viewing time. The Time.YearDay() function in Go language is used to find the day of the year as provided by “t”. Where, the range for non-leap years is [1, 365] and for leap years it is [1, 366]. 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) YearDay() int

Here, “t” is the stated time.

Return value: It returns the day of the year as specified by “t”.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// Time.YearDay() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring t in UTC
    t := time.Date(2019, 6, 5, 11,
              35, 04, 0, time.UTC)
  
    // Calling YearDay method
    yrday := t.YearDay()
  
    // Prints the day 
    // of the year as specified
    fmt.Printf("The day of the year "+
      "in the 't' specified is: %v\n", yrday)
}

chevron_right


Output:

The day of the year in the 't' specified is: 156

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// Time.YearDay() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring t in UTC
    t := time.Date(2020, 35, 60, 
       45, 67, 94, 7567, time.UTC)
  
    // Calling YearDay method
    yrday := t.YearDay()
  
    // Prints the day of the year as specified
    fmt.Printf("The day of the year in "+
      "the 't' specified is: %v\n", yrday)
}

chevron_right


Output:

The day of the year in the 't' specified is: 365

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :