How to get current year in Golang

Last updated on Jul 7, 2022 by Suraj Sharma


In this tutorial, you will learn to get the current year in golang using the in-built time module


Get current year


To get the current year in golang, we will use the Now() and Year() functions defined in the golang’s time module


Solution


package main

import (
  "fmt"
  "time"
)

func main() {
  //current date
  today := time.Now()

  //prints the current year
  fmt.Printf("current year: %d", today.Year())
}


Related Solutions


Rate this post


Suraj Sharma is the founder of Future Gen AI Services. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.