How to validate the Date

I am getting a Birthdate in String (“12345678”), I am converting that string to Date by using Time.Parse. My function works fine until this part but if someone enters a wrong birthdate then it’s not giving me an error. For example: if I enter “00000000”) then I am not gonna get any error. Can you please help me with this issue?
`
func (p *Package1) checkBirthdate {
if len(p.Birthdate) != 0 {
_, err := time.Parse(“2006-01-02”, p.Birthdate[0:4]+"-"+p.Birthdate[4:6]+"-"+p.Birthdate[6:8])
if err != nil {
fmt. Printf(“Error occured %s”, p.Birthdate)
}
} ``

Which date is this value represention? Do you mean something like 19700228 representing the 28th February, 1970?

Yes, that should be the representation.
So, like if I put “00000000” then I should get a error that this is not a valid date, or if I put “20200230” then I should get an error but I am not getting that error in my function.

I can’t reproduce the behaviour you describe. Consider this code:

package main

import (
	"fmt"
	"time"
)

func parse(s string) (time.Time, error) {
	d, err := time.Parse("2006-01-02", s)
	if err != nil {
		return d, err
	}
	return d, nil
}

func main() {
	d, err := parse("1970-02-28")
	if err != nil {
		panic(err)
	}
	fmt.Println(d)

	d, err = parse("0000-00-00")
	if err != nil {
		panic(err)
	}
	fmt.Println(d)

}

The first call to parse returns a valid date while the second call returns an error. This all works as expected.

see https://play.golang.org/p/k8hW04pz6yM

Output:

1970-02-28 00:00:00 +0000 UTC
panic: parsing time "0000-00-00": month out of range
1 Like