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
+4.6k JavaScript: Add marker function on Google Map
+5.5k Swift : Get substring with rangeOfString() function example
+5.9k Golang : Create new color from command line parameters
+16.7k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+8.2k Golang : Count leading or ending zeros(any item of interest) example
+15.7k Golang : Update database with GORM example
+13k Golang : How to get a user home directory path?
+8.9k Golang : Intercept and compare HTTP response code example
+5.3k Golang : Display advertisement images or strings on random order
+35.1k Golang : Strip slashes from string example
+30.6k Golang : Download file example
+18.3k Golang : Aligning strings to right, left and center with fill example