Golang : Get current time
Problem :
Need to get current time stamp in Go and display in string.
Solution :
Use the time.Local() and time.Format() functions.
getcurrenttime.go
package main
import (
"fmt"
"os"
"time"
)
func main() {
// get current timestamp
currenttime := time.Now().Local()
fmt.Println("Current time : ", currenttime.Format("2006-01-02 15:04:05 +0800"))
}
Execute the code
>go run getcurrenttime.go
and the output will be :
Current time : 2014-06-30 14:23:38 +0800
Take note that +0800
is the timezone in my current location. You can change it to suit your timezone.
Reference :
See also : Golang : Get file last modified date and time
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
+48.8k Golang : Upload file from web browser to server
+6.4k Golang : Process non-XML/JSON formatted ASCII text file example
+5.6k Golang : Stop goroutine without channel
+10.8k Golang : Simple File Server
+9.3k Golang : Handle sub domain with Gin
+7.5k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+6.7k Golang : Totalize or add-up an array or slice example
+11.9k Golang : How to detect a server/machine network interface capabilities?
+17.9k Golang : Read data from config file and assign to variables
+15.5k Golang : How to get Unix file descriptor for console and file
+10.5k Golang : Detect number of faces or vehicles in a photo
+12.5k Golang : Validate email address