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
+18k Golang : Check if directory exist and create if does not exist
+5.2k Golang : Dealing with backquote
+11.1k Golang : Get month name from date example
+16.3k Golang : Linked list example
+12.8k Golang : convert rune to unicode hexadecimal value and back to rune character
+10k Golang : Calculate Relative Strength Index(RSI) example
+9.4k Golang : Allow Cross-Origin Resource Sharing request
+6.3k Golang : Process json data with Jason package
+6.7k Setting $GOPATH environment variable for Unix/Linux and Windows
+8.5k Golang : Quadratic example
+7.1k Swift : Convert (cast) String to Float
+9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter