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
+3.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+7.6k Golang : "https://" not allowed in import path
+4.8k Golang : HttpRouter multiplexer routing example
+20k error: trying to remove "yum", which is protected
+8k Golang : Read data from config file and assign to variables
+6k Golang : Get local time and equivalent time in different time zone
+8.6k Golang : Read large file with bufio.Scanner cause token too long error
+7k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+5.7k Golang : Linear algebra and matrix calculation example
+2.5k Golang : Check if password length meet the requirement
+5.2k Android Studio : Password input and reveal password example
+4.5k Golang : Function wrapper that takes arguments and return result example