How to convert time.Time to string in Go? (date and time formatting)
Hello everyone! I just started learning Go, and I have a problem with converting time to string. I need to output the current date and time in the format "2024-12-25 14:30:00" (like in MySQL), but I don't understand how to do it correctly.
I tried using the standard `String()` method on `time.Time`, but it outputs something like "2024-12-25 14:30:00 +0000 UTC", and I only need year-month-day hours:minutes:seconds. Also, I heard that Go has some "magic" format with the number 2006-01-02, but it looks strange.
Here's my code:
```go
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Now:", now.String()) // outputs not what I need
}
```
Please tell me how to properly format time into a string? And why does Go use
No answers yet.
Log in to leave an answer
Blogs