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
+12.1k Golang : Save webcamera frames to video file
+17.7k Golang : [json: cannot unmarshal object into Go value of type]
+10.2k Golang : Find and replace data in all files recursively
+14.7k Golang : Reset buffer example
+26.3k Golang : Calculate future date with time.Add() function
+12.3k Golang : Validate email address
+16.4k Golang : How to implement two-factor authentication?
+16k Golang : How to reverse elements order in map ?
+12.3k Golang : Get month name from date example
+6.3k Golang : How to get capacity of a slice or array?
+4.9k Javascript : How to get width and height of a div?
+8.5k Golang : How to check if input string is a word?