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
+6.9k Golang : get the current working directory of a running program
+19.9k Golang : For loop continue,break and range
+9.6k Golang : How to transmit update file to client by HTTP request example
+33.6k Golang : Upload and download file to/from AWS S3
+30k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+5.4k Golang : Break string into a slice of characters example
+3.9k Golang : A program that contain another program and executes it during run-time
+11.2k Golang : How to check if a string starts or ends with certain characters or words?
+22.2k Golang : Print out struct values in string format
+4.9k Golang : Find change in a combination of coins example
+21.6k Golang : Test file read write permission example
+12.3k Golang : Read XML elements data with xml.CharData example