String to time conversion. Time

Asked

Viewed 40 times

0

Good afternoon! I wanted to know how to convert/transform a String date into (dd/mm/yyyy) time format. Time

1 answer

0

There is the function Parse for this purpose (convert a human date value to the team. Time).

It is important that you are familiar with the format used, this can be visualized here.


The format of dd/mm/yyyy is equivalent to 02/01/2006, as mentioned above. The time.Parse requires two values: the format and the date. Then just use:

formato := `02/01/2006` // Este é o DD/MM/YYYY
data := `18/03/2020`    // Este é a data que quer converter para time.Time

t, err := time.Parse(formato, data)
if err != nil {
    log.Fatal("A data inserida é inválida") 
}

If you want the format to be: d/m/yyyy. More clearly, if you want to accept a date like: 1/1/2000 (instead of only accepting 01/01/2000), you should use the 2/1/2006 as a format (instead of 02/01/2006).

Finally: if you have to use 0 (as 01/01/2000) use 02/01/2006, if you don’t use 2/1/2006.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.