#Parse dates correctly

17 messages · Page 1 of 1 (latest)

clear prism
#

If I have a slice of strings like this:

2020_01_05
2019_6_04
2019_6_24
2019_06_04
2019_06_4
2019_06_8
2020_1_05
2020_01_7
2020_02_05```

And I want to parse them like this form:
```2019/6/8
2020/1/7
2020/2/5
2019/6/4
2020/1/5
2019/6/24```

How can I do this in golang?
clear prism
#

someone deleted?

proper mural
#

strings.Split

#

strconv.Atoi

#

(fmt.Sprintf)

#

time.Date/time.Parse/time.ParseInLocation

clear prism
#

got it, thank you

proper mural
#

time.Time.Format

#

for formating tobthe desired layout

#

tho Go time layouts are somewhat tricky, because they expect you to use a concrete date and time to be used for defining the layout string

#

a specific date and time

#

the dates in these constant values are the ones you need to use.
like 06 for YY
2006 for YYYY
2 for D
02 for DD
1 for M
01 for MM
and the same with hours, minutes, seconds

#

"2006/2/1" would be a format string for your layout above

flint lily
#

I would perfer time.Parse in a for loop

#

and check the error in the for loop

proper mural
#

the input has different formats