Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
Feeling bored today and just want to play around with date formats. It seems that Golang's time package does not have the formats for dd-mm-yyyy.
Below is an example code on how to change the American style date format to British/Commonwealth/European/yyyy-mm-dd style date formats. It also shows you how to change the day month year separator as well.
Here you go!
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
// mm-dd-yyyy for USA/American
fmt.Println("mm-dd-yyyy date format : ", now.Format("01-02-2006")) // <-- CORRECT
// change to British/Commonwealth/European countries date format
// from mm-dd-yyyy to dd-mm-yyyy
fmt.Println("dd-mm-yyyy date format : ", now.Format("02-01-2006"))
// play around with 01 and 02 and see how it goes...
fmt.Println("yyyy-mm-dd date format : ", now.Format("2006-01-02"))
fmt.Println("yyyy-dd-mm date format : ", now.Format("2006-02-01"))
// you can change the separator too!
fmt.Println("dd.mm.yyyy date format : ", now.Format("02.01.2006"))
fmt.Println("dd/mm/yyyy date format : ", now.Format("02/01/2006"))
}
Happy coding!
Sample output :
Before : 2015-08-28 13:59:53.975870721 +0800 SGT
mm-dd-yyyy date format : 08-28-2015
dd-mm-yyyy date format : 28-08-2015
yyyy-mm-dd date format : 2015-08-28
yyyy-dd-mm date format : 2015-28-08
dd.mm.yyyy date format : 28.08.2015
dd/mm/yyyy date format : 28/08/2015
Reference :
See also : Golang : Change date format to yyyy-mm-dd
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
+22.3k Golang : Set and Get HTTP request headers example
+8.5k Golang : Executing and evaluating nested loop in html template
+6k Golang : How to get capacity of a slice or array?
+22.8k Golang : Randomly pick an item from a slice/array example
+15.1k Golang : Delete certain files in a directory
+11.1k Golang : Characters limiter example
+3.5k Java : Random alphabets, alpha-numeric or numbers only string generator
+8.9k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+16.3k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+14.3k Golang : Overwrite previous output with count down timer
+14.2k Golang : How to filter a map's elements for faster lookup
+11k Golang : Fix fmt.Scanf() on Windows will scan input twice problem