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
+12.8k Python : Convert IPv6 address to decimal and back to IPv6
+5.7k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+9k Golang : Capture text return from exec function example
+7.9k Golang : Grayscale Image
+13.3k Golang : Verify token from Google Authenticator App
+28.4k Golang : Change a file last modified date and time
+13.1k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+14k Javascript : Prompt confirmation before exit
+9.3k Golang : Play .WAV file from command line
+12.3k Golang : Validate email address
+20.4k Golang : Pipe output from one os.Exec(shell command) to another command
+8k Golang : HTTP Server Example