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
+39.1k Golang : How to read CSV file
+11.6k Golang : Simple file scaning and remove virus example
+10.9k Golang : How to transmit update file to client by HTTP request example
+12.8k Golang : http.Get example
+8.4k Golang : Ackermann function example
+13.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+23.4k Golang : Check if element exist in map
+7.3k Golang : Calculate how many weeks left to go in a given year
+12.3k Golang : Encrypt and decrypt data with x509 crypto
+5.7k Javascript : How to replace HTML inside <div>?
+13.4k Golang : error parsing regexp: invalid or unsupported Perl syntax
+22.6k Golang : Strings to lowercase and uppercase example