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
+37.7k Golang : Read a text file and replace certain words
+30k Golang : How to verify uploaded file is image or allowed file types
+11.7k Golang : How to parse plain email text and process email header?
+9.5k Golang : Detect number of active displays and the display's resolution
+7.9k Golang : Variadic function arguments sanity check example
+9.1k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+9.1k Golang : How to get garbage collection data?
+6.8k Web : How to see your website from different countries?
+11.4k SSL : The certificate is not trusted because no issuer chain was provided
+11.4k Golang : Display a text file line by line with line number example
+4.8k Golang : micron to centimeter example
+11.2k Android Studio : Create custom icons for your application example