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
+17.6k Golang : Populate dropdown with html/template example
+17.1k Golang : Implement getters and setters
+18k Golang : How to get own program name during runtime ?
+11.6k Golang : Transform comma separated string to slice example
+16.5k Golang : How to log each HTTP request to your web server?
+26.1k Golang : Record voice(audio) from microphone to .WAV file
+7.6k Golang : Set or add headers for many or different handlers
+10.7k Golang : Decompress zlib file example
+24.6k Golang : missing Mercurial command
+13.1k Golang : How to determine if user agent is a mobile device example
+4.3k Golang *File points to a file or directory ?