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
+10.8k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+20.1k SSL : How to check if current certificate is sha1 or sha2
+8.4k Golang : Format strings to SEO friendly URL example
+6.5k Golang : Shuffle strings array
+4.1k Unix/Linux : secure copying between servers with SCP command examples
+4.9k Facebook : How to force facebook to scrape latest URL link data?
+10.4k Golang : Get remaining text such as id or filename after last segment in URL path
+12.2k Golang : Verify token from Google Authenticator App
+35.2k Golang : Convert(cast) int64 to string
+8.3k Golang : ffmpeg with os/exec.Command() returns non-zero status
+6.5k Golang : Convert(cast) io.Reader type to string
+18.2k Golang : Archive directory with tar and gzip