Golang : Change date format to yyyy-mm-dd
Problem :
Your Golang program is producing date in long form that look like this :
2015-08-11 17:21:52.776537624 +0800 MYT
and you want to change the format to short form - yyyy-mm-dd
. How to do that?
Solution :
Use the time.Time.Format()
function. For example :
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Before : ", now)
// reduce the date format
// remember NOT to use 2006-01-01 or 02-02 or same digit
// for month and date. Will cause weird date result
//fmt.Println(now.Format("2006-01-01")) <--- WRONG
fmt.Println("After : ", now.Format("2006-01-02")) // <-- CORRECT
}
Sample output :
Before : 2015-08-11 17:29:07.626918059 +0800 MYT
After : 2015-08-11
See also : Golang : Date and Time formatting
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
+16.4k Android Studio : AlertDialog and EditText to get user string input example
+10.9k Golang : reCAPTCHA example
+9.3k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+2.7k Detect if Google Analytics and Developer Media are loaded properly or not
+15.6k Golang : Fix cannot download, $GOPATH not set error
+6.6k Golang : Count leading or ending zeros(any item of interest) example
+7.2k Golang : does not implement flag.Value (missing Set method)
+6.3k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+11.6k Golang : Strings comparison
+30.4k Golang : Validate IP address
+5.4k Golang : alternative to os.Exit() function
+5.3k Golang : Array mapping with Interface