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
+9.8k Golang : How to determine if a year is leap year?
+4k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+4.2k Apt-get to install and uninstall Golang
+3.5k Unix/Linux : How to test user agents blocked successfully ?
+4.4k Golang : Output or print out JSON stream/encoded data
+19k Golang : dial tcp: too many colons in address
+3.9k Restart Apache or Nginx web server without password prompt
+3.7k Golang : Struct field tags and what is their purpose?
+3.7k Golang : What is StructTag and how to get StructTag's value?
+5.2k Golang : Command line ticker to show work in progress
+4.8k Golang : Gorrila set route name and get the current route name
+17.3k Golang : Get ASCII code from a key press(cross-platform) example