Golang program to get the current day of the year out of 365


In Golang we can use functions like YearDay and Sub as well as arithmetic operation method to get the current day of the year out of 365. The current time is obtained using the Now function from the time package and YearDay() function is used to obtain the current day of the year out of 365.

Method 1: Using YearDay and Sub Functions

In this method, the number of days between the current date and start of the year are calculated using currentTime.Sub(start_of_year).Hours() / 24 as well as simple YearDay method.

Syntax

func Now() Time

The Now() function is defined in time package. This function generates the current local time. To use this function, we have to first import the time package in our program.

func sub()

This function is part of the time package. It is used to calculate the duration between the two time.Time values.

Algorithm

  • Step 1 − Import the required packages in the program

  • Step 2 − Create a main function

  • Step 3 − In the main get the current day of the year using internal built-in functions

  • Step 4 − Print the current day of the year on the console using Printf function from the fmt package

Example 1

In this example, we will write a Golang program to find the current time of the year using YearDay function

package main

import (
   "fmt"
   "time"
)

func main() {
   currentTime := time.Now()
   yearDay := currentTime.YearDay()
   fmt.Printf("Today is day %d of the year.\n", yearDay)
}

Output

Today is day 93 of the year.

Example 2

In this example, we will write a Golang program to find the current time of the year using Sub function

package main

import (
   "fmt"
   "time"
)

func main() {
   currentTime := time.Now()
   start_of_year := time.Date(currentTime.Year(), 1, 1, 0, 0, 0, 0, currentTime.Location())
   year_day := int(currentTime.Sub(start_of_year).Hours()/24) + 1
   fmt.Printf("Today is day %d of the year.\n", year_day)
}

Output

Today is day 93 of the year.

Method 2: Using Arithmetic Operators

In this method, we will write a Golang program to get the current day of the year out of 365 using arithmetic operations. The arithmetic operation used here is modulo operator to check whether the year is a leap year or not.

Algorithm

  • Step 1 − This program imports the package fmt and time in the program where fmt helps in the formatting of the input and Output and time helps in getting the time

  • Step 2 − Create a main function and in that function get the current time using Now function from the time package

  • Step 3 − Then, calculate the day of the year using the YearDay function with the current time

  • Step 4 − In this step, get the year of the current time using Year function with the current time

  • Step 5 − Then, check if the year is a leap year or not by getting its modulo with 4, 100 and 400

  • Step 6 − If it’s a leap year and the day of the year is greater than equal to 60 then increment day of the year with 1

  • Step 7 − Finally, print the day of the year using Println function from the fmt package where ln means new line

Example

In the following example, we are going to use arithmetic operators to get the current day of the year out of 365 in go programming language

package main

import (
   "fmt"
   "time"
)

func main() {
	
   Now := time.Now()

	
   day_of_year := Now.YearDay()
   year := Now.Year()

	
   leap_year := (year%4 == 0 && year%100 != 0) || year%400 == 0

	
   if leap_year && day_of_year >= 60 {
      day_of_year++
   }

   fmt.Printf("Today is the day %d of the year.\n", day_of_year)
}

Output

Today is the day 93 of the year.

Conclusion

We executed and compiled the program of getting the current day of the year out of 365 using three examples. In the first example, we used YearDay function and in the second example we obtained the current day of the year by finding the number of days between the current date and start of the year for which we used sub method and in the third example we used arithmetic operations.

Updated on: 03-Apr-2023

489 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements