Skip to content
Home » Program to convert a date-time object into a string in Golang

Program to convert a date-time object into a string in Golang

Learn about Program to convert a date-time object into a string in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Program to convert a date-time object into a string in Golang

// Golang program to convert a date object into a string

package main

import "fmt"
import "time"

func main() {
	var strDate string
	date := time.Date(2021, 3, 20, 10, 5, 20, 0, time.UTC)

	strDate = date.String()

	fmt.Printf("Result: %s", strDate)
}

In the above program, we imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. Here, we created a date-time object using Date() function. Then we converted the date-time object into the string using the String() function

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to compare two dates in Golang
How to print tomorrow’s date in Golang