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.7k Golang : Check if password length meet the requirement
+15.8k Golang : How to login and logout with JWT example
+15.2k Golang : How to add color to string?
+6.3k Unix/Linux : Use netstat to find out IP addresses served by your website server
+9k Golang : Build and compile multiple source files
+35.6k Golang : Smarter Error Handling with strings.Contains()
+6.3k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+7.5k Golang : Rot13 and Rot5 algorithms example
+10.9k Golang : How to transmit update file to client by HTTP request example
+13.5k Golang : Increment string example
+22.6k Generate checksum for a file in Go