Things about time.Parse of Golang

201 阅读1分钟

We often use time package in Golang to display or parse date time. It is so common that we may ignore some import things when use it.

  1. the first parameter of time.Parse, namely layout, must equal to 20060102 15:04:05, otherwise it will lead to some puzzling questions. So to be correct we may write the code like this,
time.Parse("20060102", date_str_val)
date, err := time.Parse("20060102 15:04:05", "20210908 11:12:34")
  1. the timezone can be set by time.LoadLocation("Asia/Shanghai"), in this way time.ParseInLocation can be called conviniently later by passing the third parameter to time.Local. In summary When processing time or date, we must pay attention to the timezone.