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
+15.7k Golang : Get file permission
+15k Golang : Delete certain files in a directory
+4.5k Fix Google Analytics Redundant Hostnames problem
+9.4k Golang : Load ASN1 encoded DSA public key PEM file example
+34.9k Golang : Integer is between a range
+16.5k Golang : How to generate QR codes?
+10.4k Golang : Simple File Server
+22.2k Generate checksum for a file in Go
+6.9k CloudFlare : Another way to get visitor's real IP address
+13.5k Golang : How to check if a file is hidden?
+12.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+17.1k Golang : Get future or past hours, minutes or seconds