How to Convert Time to String in Golang


If you need to convert Time to a String in Go, then you can do one of the following:

Option 1 – Using time.Now

package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    fmt.Println("Time: ", currentTime.String())
}

Option 2 – Using time.Time.String()

package main

import (
    "fmt"
    "time"
)

func main() {
    Time := time.Date(2022, 03, 28, 03, 50, 16, 0, time.UTC)
    t := Time.String()
    fmt.Printf("Time without the nano seconds: %v\n", t)
}