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
+32.9k Golang : Strip slashes from string example
+10k How to tell if a binary(executable) file or web application is built with Golang?
+10k Golang : Surveillance with web camera and OpenCV
+10.3k Golang : Change date format to yyyy-mm-dd
+4.3k Golang : Grab news article text and use NLP to get each paragraph's sentences
+20.2k Golang : How to run Golang application such as web server in the background or as daemon?
+5.6k Golang : Decode XML data from RSS feed
+8.5k Golang : Translate language with language package example
+20.7k Golang : Set and Get HTTP request headers example
+4.1k Unix/Linux/MacOSx : How to remove an environment variable ?
+9.6k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example