How to Convert Time to String in Golang

0 min read 77 words

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)
}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags