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.9k Golang : Add ASCII art to command line application launching process
+7.5k Golang : Executing and evaluating nested loop in html template
+6.7k Golang : Sort words with first uppercase letter
+7.8k Golang : HTTP Routing with Goji example
+9.3k Golang : Simple File Server
+25.8k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+4.8k Golang : Denco multiplexer example
+29.7k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+12k Golang : Read XML elements data with xml.CharData example
+9.2k Golang : ISO8601 Duration Parser example
+14.2k Golang : invalid character ',' looking for beginning of value
+4.4k Golang : Intercept, inject and replay HTTP traffics from web server