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
+16.4k Golang : How to extract links from web page ?
+11k Golang : Get UDP client IP address and differentiate clients by port number
+11.6k CodeIgniter : Import Linkedin data
+7.6k Golang : Dealing with struct's private part
+11.4k Golang : Characters limiter example
+16k Golang : Get current time from the Internet time server(ntp) example
+7.4k Golang : How to convert strange string to JSON with json.MarshalIndent
+15.1k Golang : Search folders for file recursively with wildcard support
+11k Golang : Removes punctuation or defined delimiter from the user's input
+5.5k How to check with curl if my website or the asset is gzipped ?
+7.3k CloudFlare : Another way to get visitor's real IP address
+6.1k Golang : How to verify input is rune?