The first parameter layout cannot be specified at any time

For example, 2019-05-16 23:23:45

It is possible to report err, using “2006-01-02 15:04:05”. This will not report an error.

  • Solution:

The string to time:

time, err := time.ParseInLocation("2006-01-02 15:04:05", req.UploadTime, time.Local)
Copy the code

I have time to look at the source code here, analyze why will parse failure. To record the first


Problem solved, because it’s Go’s birthday… The matching strategy also matches on this date, as expected. How else do you know if each number corresponds to a month or a day or something? Match the required format with a fixed date. In fact, it also meets the design expectations. Kind of a little egg.

  • Time, String interconversion method:
func Time2Str(t *time.Time) string {
	return t.Format("The 2006-01-02 15:04:05")}func Str2Time(timeStr string) time.Time {
	t, _ := time.ParseInLocation("The 2006-01-02 15:04:05", timeStr, time.Local)
	return t
}
Copy the code