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.5k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+16.9k Golang : How to tell if a file is compressed either gzip or zip ?
+8.9k Golang : Gonum standard normal random numbers example
+16.5k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+7.1k Android Studio : How to detect camera, activate and capture example
+14.9k Golang : Accurate and reliable decimal calculations
+5.6k Golang : Launching your executable inside a console under Linux
+15.4k Golang : Get digits from integer before and after given position example
+16.5k Golang : read gzipped http response
+6k Javascript : Generate random key with specific length
+5.7k Golang : Shuffle array of list
+21.2k Golang : GORM create record or insert new record into database example