Golang : Convert int(year) to time.Time type
Problem :
You have integer value such as a year and you want to convert the integer to time.Time type. How to do that?
Solution :
Parse the integer with time.Date()
function and return the time.Time type
package main
import (
"fmt"
"time"
)
func YearTime(y int) time.Time {
// convert int to Time - use the last day of the year, which is 31st December
t := time.Date(y, time.December, 31, 0, 0, 0, 0, time.Local)
return t
}
func main() {
yTime := YearTime(2015)
fmt.Println("Year is : ", yTime.Year())
}
Output :
Year is : 2015
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+4.2k Golang *File points to a file or directory ?
+5.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+12.5k Javascript : Prompt confirmation before exit
+4.1k Python : Convert(cast) bytes to string example
+7.3k Golang : On lambda, anonymous, inline functions and function literals
+4.3k Unix/Linux/MacOSx : How to remove an environment variable ?
+12.4k Golang : Activate web camera and broadcast out base64 encoded images
+15.6k Golang : Get number of CPU cores
+18.2k Golang : Check if os.Stdin input data is piped or from terminal
+11.3k Golang : Covert map/slice/array to JSON or XML format
+14.7k Golang : How to check if input from os.Args is integer?