How to check if date is in right format?

How can I check if the date is in the right format? For example, I am making API calls if the DOB is in the right format, can you plz tell me which package I should choose for that?

Thank you,

Like this?

package main

import (
    "fmt"
    "time"
)

// IsCorrectDate return whether the date is in correct format
func IsCorrectDate(format, date string) bool {
    _, err := time.Parse(format, date)
    if err != nil {
        return false
    }
    return true
}

func main() {
    // correct DOB format is YYYY-MM-DD
    dobFormat := "2006-01-02"

    dob1 := "1984-12-22"
    dob2 := "12-22-1984"

    fmt.Println(IsCorrectDate(dobFormat, dob1))
    fmt.Println(IsCorrectDate(dobFormat, dob2))
}
2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.