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
+19.6k Golang : How to Set or Add Header http.ResponseWriter?
+11.1k Golang : Create S3 bucket with official aws-sdk-go package
+6.5k Golang : Break string into a slice of characters example
+10.3k Golang : Print how to use flag for your application example
+16.5k Golang :Trim white spaces from a string
+12.9k Golang : Listen and Serve on sub domain example
+18.1k Golang : Defer function inside init()
+15.9k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+15.4k Golang : Get HTTP protocol version example
+6.9k Golang : Muxing with Martini example
+13.6k Golang : Get constant name from value
+22.5k Golang : Convert seconds to minutes and remainder seconds