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
+45.9k Golang : Encode image to base64 example
+17.3k Golang : Iterate linked list example
+77.3k Golang : How to return HTTP status code?
+13.7k Golang : Google Drive API upload and rename example
+12.3k Golang : zlib compress file example
+4.1k Javascript : How to show different content with noscript?
+9.4k Golang : List available AWS regions
+14.1k Golang : Find network of an IP address
+15.7k Golang : Get sub string example
+11.9k Golang : Flush and close file created by os.Create and bufio.NewWriter example
+6.8k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+7.4k Golang : Lock executable to a specific machine with unique hash of the machine