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
+7.3k Golang : alternative to os.Exit() function
+16.2k Golang : How to check if input from os.Args is integer?
+9.4k Golang : How to protect your source code from client, hosting company or hacker?
+10.3k Golang : Detect number of faces or vehicles in a photo
+27.8k PHP : Count number of JSON items/objects
+42k Golang : How do I convert int to uint8?
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+6k Golang : Compound interest over time example
+5.4k Javascript : Shuffle or randomize array example
+4.8k Facebook : How to place save to Facebook button on your website
+9.3k Golang : Generate random Chinese, Japanese, Korean and other runes