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
+8.2k Golang : Find relative luminance or color brightness
+6.1k Linux/Unix : Commands that you need to be careful about
+12.4k Golang : Forwarding a local port to a remote server example
+20.2k Golang : Check if os.Stdin input data is piped or from terminal
+16.5k Golang : Delete files by extension
+15.6k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+21.8k Golang : Convert string slice to struct and access with reflect example
+8.4k Golang : Generate Datamatrix barcode
+7k Golang : Gargish-English language translator
+18.5k Golang : Send email with attachment
+18.1k Golang : Put UTF8 text on OpenCV video capture image frame
+15.3k Golang : Get all local users and print out their home directory, description and group id