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
+8.6k Golang : Eroding and dilating image with OpenCV example
+12.2k Golang : reCAPTCHA example
+6.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+8k Golang : How to extract video or image files from html source code
+13.3k Golang : How to shuffle elements in array or slice?
+8.9k Golang : Get current, epoch time and display by year, month and day
+16.5k Convert JSON to CSV in Golang
+8.8k Golang : Check if user agent is a robot or crawler example
+6.8k Javascript : How to check a browser's Do Not Track status?
+7.2k Your page has meta tags in the body instead of the head
+8.3k Golang : How to generate Code 39 barcode?
+4.4k Golang : Reclaim memory occupied by make() example