Golang time.ParseInLocation() function example
package time
Golang time.ParseInLocation() function usage example
package main
import (
"fmt"
"time"
)
func main() {
layOut := "2006-01-02 15:04:05" // no time zone
date, err := time.ParseInLocation(fmt.Sprintf(layOut), "2015-03-08 15:02:01", time.Now().Location())
if err != nil {
fmt.Println(err)
}
fmt.Println(date)
// layOutTimeZone := "2012-07-09 17:04:05 -0700"
layOutTimeZone := "Jan 2, 2006 at 3:04pm (MST)" // with different layout just for fun
dateTZ, err := time.ParseInLocation(fmt.Sprintf(layOutTimeZone), "Jul 9, 2012 at 5:02am (CEST)", time.Now().Location())
if err != nil {
fmt.Println(err)
}
fmt.Println(dateTZ)
}
Output :
2015-03-08 15:02:01 +0800 MYT
2012-07-09 05:02:00 +0000 CEST
Advertisement
Something interesting
Tutorials
+7k Web : How to see your website from different countries?
+13.2k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+5.3k Javascript : Change page title to get viewer attention
+15.6k Golang : ROT47 (Caesar cipher by 47 characters) example
+8.6k Golang : Progress bar with ∎ character
+7.5k Golang : Process json data with Jason package
+10.7k Golang : Underscore string example
+8.7k Golang : Find duplicate files with filepath.Walk
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+12k Golang : Clean formatting/indenting or pretty print JSON result
+5.7k Linux/Unix/PHP : Restart PHP-FPM
+19.3k Golang : Calculate entire request body length during run time