Golang time.Parse() function example
package time
Golang time.Parse() function usage example
package main
import (
"fmt"
"time"
)
func main() {
// get current timestamp
currentTime := time.Now().Local()
fmt.Println(currentTime)
// don't like the standard format ?
// change it
newFormat := currentTime.Format("2006-01-02 15:00:00 +0800")
fmt.Println(newFormat)
fmt.Println("===============================")
// Changing time layout(form)
str := "Dec 29, 2014 at 7:54pm (SGT)"
newLayout := "Jan 2, 2006 at 3:04pm (MST)"
newTime, _ := time.Parse(newLayout, str)
fmt.Println("1 : ", newTime)
shortForm := "2006-Jan-02 (SGT)"
str = "2014-Dec-29 (SGT)"
newTime, _ = time.Parse(shortForm, str)
fmt.Println("2 : ",newTime)
}
2009-11-10 23:00:00 +0000 UTC
2009-11-10 23:00:00 +0800
===============================
1 : 2014-12-29 19:54:00 +0000 SGT
2 : 2014-12-29 00:00:00 +0000 UTC
Reference :
Advertisement
Something interesting
Tutorials
+7.1k Golang : Get environment variable
+41.4k Golang : Convert string to array/slice
+7.6k Golang : Convert(cast) io.Reader type to string
+7k Golang : Takes a plural word and makes it singular
+19.5k Golang : How to Set or Add Header http.ResponseWriter?
+14k Golang : Google Drive API upload and rename example
+11.3k Golang : Intercept and process UNIX signals example
+14.6k Golang : Missing Bazaar command
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+4.6k MariaDB/MySQL : How to get version information
+14.4k Golang : Find network of an IP address