how to convert string to date in golang?

Hi 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

Try it Yourself

I hope it helps you, All the best 👍.

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts