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
+15.9k Golang : Get digits from integer before and after given position example
+6.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+9.3k Golang : How to get garbage collection data?
+4.7k Linux : sudo yum updates not working
+5.8k Unix/Linux : Get reboot history or check when was the last reboot date
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+7.5k Golang : Rename part of filename
+7k Web : How to see your website from different countries?
+14.1k Golang : Reverse IP address for reverse DNS lookup example
+12.1k Golang : Save webcamera frames to video file
+14.1k Golang : Compress and decompress file with compress/flate example
+6.9k Mac OSX : Find large files by size