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
+5k PHP : See installed compiled-in-modules
+29k Golang : JQuery AJAX post data to server and send data back to client example
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+10.4k Golang : Underscore string example
+9.4k Golang : Find correlation coefficient example
+10.2k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine
+19.4k Golang : Set or Add HTTP Request Headers
+15k Golang : Get timezone offset from date or timestamp
+7.1k Golang : Scanf function weird error in Windows
+8.6k Golang : GMail API create and send draft with simple upload attachment example
+14k Android Studio : Use image as AlertDialog title with custom layout example
+7.8k Golang : Multiplexer with net/http and map