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
+5.6k Golang : Calculate pivot points for a cross
+8k Golang : Serving HTTP and Websocket from different ports in a program example
+4.6k Golang : Get S3 or CloudFront object or file information
+9.4k Golang : Select region of interest with mouse click and crop from image
+6.8k Golang : Sort words with first uppercase letter
+5.8k Default cipher that OpenSSL used to encrypt a PEM file
+4.6k Unix/Linux/MacOSx : Get local IP address
+20.1k Golang : Convert(cast) string to rune and back to string example
+10.4k Golang : Secure file deletion with wipe example
+9.3k Generate Random number with math/rand in Go
+9.9k Golang : Read until certain character to break for loop
+16.4k Golang : Upload/Receive file progress indicator