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
+17.5k Unmarshal/Load CSV record into struct in Go
+4.7k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+6.5k Golang : How to convert strange string to JSON with json.MarshalIndent
+22.2k Golang : Print out struct values in string format
+6.5k Golang : Handling Yes No Quit query input
+11.6k Golang : Get terminal width and height example
+7.9k Golang : Heap sort example
+4k Golang : Customize scanner.Scanner to treat dash as part of identifier
+3.6k Linux/MacOSX : Search and delete files by extension
+9.2k Golang : Text file editor (accept input from screen and save to file)
+9.6k Golang : Fix go.exe is not compatible with the version of Windows you're running
+5k Fontello : How to load and use fonts?