jeudi 10 février 2022

what is the best way to check if a string is date value?

Reflection does not seem to be working for time. What is the best approach here?

package main

import (
    "fmt"
    "reflect"
    "time"
)

func main() {
    stringDate := "07/26/2020"

    // parse string date to golang time
    t, _ := time.Parse("01/02/2006", stringDate)

    ts := ""

    ts = t.String()

    v := reflect.ValueOf(ts)

    fmt.Println(ts) // prints "2020-07-26 00:00:00 +0000 UTC"

    fmt.Println(v.Type()) //  prints "string". How do I get this to time.Time ?

}




Aucun commentaire:

Enregistrer un commentaire