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
+6.6k Default cipher that OpenSSL used to encrypt a PEM file
+30.1k Golang : How to redirect to new page with net/http?
+4.7k JQuery : Calling a function inside Jquery(document) block
+8.1k Golang : Count leading or ending zeros(any item of interest) example
+10.2k Swift : Convert (cast) String to Integer
+8.2k Golang : Generate Datamatrix barcode
+7.3k SSL : How to check if current certificate is sha1 or sha2 from command line
+26.5k Golang : Convert file content into array of bytes
+21.5k Golang : Convert string slice to struct and access with reflect example
+12k Golang : List running EC2 instances and descriptions
+14.2k Golang : How to determine if user agent is a mobile device example
+30.7k Golang : Calculate percentage change of two values