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
+2.6k Java : Get FX sentiment from website example
+4.4k Javascript : How to loop over and parse JSON data?
+14.5k Golang : Read large file with bufio.Scanner cause token too long error
+8.1k Golang : How to check if a string with spaces in between is numeric?
+30.1k Golang : Math pow(the power of x^y) example
+3.8k Python : Find out the variable type and determine the type with simple test
+5.7k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+26.4k Golang : Move file to another directory
+5.7k Restart Apache or Nginx web server without password prompt
+7.9k Golang : automatically figure out array length(size) with three dots
+4.8k List of Golang XML tutorials