how to convert string to date in golang?
September 19, 2022Hi Friends 👋,
Welcome To aGuideHub! ❤️
To convert string to date in golang, just use time.Parse()
method and pass your string with desired date format, it will convert string into date.
Follow the below tutorial if you are struggling with installing GO in windows.
https://aguidehub.com/blog/how-to-install-golang-in-windows/
Today, I will show you how do I convert string to date in golang, as above mentioned I’m going to use time.Parse()
way.
The time
package provide Parse()
method.
Let’s start our Golang convert string to date example
Convert whole string into date example
main.go
package main
import (
"fmt"
"time"
)
func main() {
dateString := "2022-10-01"
date, error := time.Parse("2006-01-02", dateString)
if error != nil {
fmt.Println(error)
return
}
fmt.Println(date)
}
In the above example, we have converted string to date and printed in golang console. let’s check the output.
Output
2022-10-01 00:00:00 +0000 UTC
I hope it helps you, All the best 👍.