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
+30k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+19.1k Golang : How to run Golang application such as web server in the background or as daemon?
+5.6k Golang : Gorrila mux.Vars() function example
+14.8k Golang : Calculate entire request body length during run time
+3.5k Python : Convert(cast) bytes to string example
+6.3k Golang : Check if integer is power of four example
+5.5k Golang : Validate credit card example
+10.7k Golang : Read XML elements data with xml.CharData example
+14.1k Golang : Check if IP address is version 4 or 6
+13.3k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+6.6k Golang : Find the length of big.Int variable example
+10.4k Golang : convert(cast) string to integer value