Golang : Date and Time formatting
Our civilizations won't progress till this stage without proper understanding, parsing and organising our lives base on date and time. Golang's time package has the functions to help us to deal with time and date.
Here is a example of parsing time as string to timestamp and changing the time format.
package main
import (
"fmt"
"time"
)
func main() {
// get current timestamp
currentTime := time.Now().Local()
fmt.Println(currentTime)
// don't like the standard format ?
// change it
newFormat := currentTime.Format("2006-01-02 15:00:00 +0800")
fmt.Println(newFormat)
fmt.Println("===============================")
// Changing time layout(form)
str := "Dec 29, 2014 at 7:54pm (SGT)"
newLayout := "Jan 2, 2006 at 3:04pm (MST)"
newTime, _ := time.Parse(newLayout, str)
fmt.Println(newTime)
}
Output :
2014-12-29 12:04:53.531591733 +0800 SGT
2014-12-29 12:00:00 +0800
===============================
2014-12-29 19:54:00 +0800 SGT
Reference :
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
+5.9k Facebook : How to force facebook to scrape latest URL link data?
+7k Golang : Levenshtein distance example
+25.6k Golang : convert rune to integer value
+9k Golang : Capture text return from exec function example
+7.4k Golang : Check to see if *File is a file or directory
+6.1k Golang : Grab news article text and use NLP to get each paragraph's sentences
+29.5k Golang : Saving(serializing) and reading file with GOB
+14.1k Golang : Check if a file exist or not
+12.9k Golang : Convert IPv4 address to packed 32-bit binary format
+7.7k Golang : How to execute code at certain day, hour and minute?
+12.8k Golang : Add ASCII art to command line application launching process
+12.1k Golang : Perform sanity checks on filename example