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
+8.6k Linux/Unix : fatal: the Postfix mail system is already running
+20k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+15.5k Golang : invalid character ',' looking for beginning of value
+10.8k Golang : Underscore string example
+5.3k Golang : The Tao of importing package
+22.3k Golang : Securing password with salt
+14.1k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+8.3k Android Studio : Rating bar example
+6.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+10.1k Golang : Channels and buffered channels examples
+6.4k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+10.3k Golang : How to get quoted string into another string?