英文:
How to parse time with unique format in golang?
问题
我有一个来自Excel的日期时间,格式为18/09/21 14.56。我想解析并将其格式化为不同的格式2006-01-02 hh:mm:ss。
在Go语言中,可以解析格式为18/09/21 14.56的时间,然后将其格式化为例如2006-01-02 hh:mm:ss的格式。
英文:
I have date time with format 18/09/21 14.56 from excel. I want to parse and  format to different format 2006-01-02 hh:mm:ss
Is possible to parse time with format 18/09/21 14.56 in golang and then format it to eg. 2006-01-02 hh:mm:ss
答案1
得分: 1
Golang使用基于示例的模板来进行Parse和Format。
01   -> 带零前缀的月份
02   -> 带零前缀的日期
06   -> 年份(最后两位)
15   -> 小时(24小时制)
04   -> 带零前缀的分钟
05   -> 带零前缀的秒钟
2006 -> 完整的年份
t, _ := time.Parse("02/01/06 15.04", "18/09/21 14.56")
t.Format("2006-01-02 15:04:05") // 2021-09-18 14:56:00
更多布局选项请参考 https://stackoverflow.com/a/69338568/12301864
英文:
Golang use example based template for Parse and Format.
01   -> month with zero prefix
02   -> day with zero prefix
06   -> year (last two digits)
15   -> hour (24h based)
04   -> minutes with zero prefix
05   -> seconds with zero prefix
2006 -> long year
t, _ := time.Parse("02/01/06 15.04", "18/09/21 14.56")
t.Format("2006-01-02 15:04:05") // 2021-09-18 14:56:00
For more layout options see https://stackoverflow.com/a/69338568/12301864
答案2
得分: 0
func TestTime(t *testing.T) {
	tm, err := time.Parse("06/01/02 15.04", "18/09/21 14.56")
	if err != nil {
		return
	}
	log.Println(tm.Format("2006-01-02 15:04:05"))
}
func TestTime(t *testing.T) {
	tm, err := time.Parse("06/01/02 15.04", "18/09/21 14.56")
	if err != nil {
		return
	}
	log.Println(tm.Format("2006-01-02 15:04:05"))
}
英文:
func TestTime(t *testing.T) {
	tm, err := time.Parse("06/01/02 15.04", "18/09/21 14.56")
	if err != nil {
		return
	}
	log.Println(tm.Format("2006-01-02 15:04:05"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论