如何在golang中转换日期格式? [英] How to convert date format in golang?
本文介绍了如何在golang中转换日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将日期格式从 2010-01-23 11:44:20
转换为 10年1月23日11:44
在golang中。我尝试了时间包的一些功能,但无法完成。
I would like to convert date format from 2010-01-23 11:44:20
to Jan 23 '10 at 11:44
in golang. I tried few functions from time package but couldn't make it.
有人可以帮我解决这个问题吗?
Could someone help me with this?
推荐答案
时间包的 解析
和 格式
将其转换为所需的格式文本格式。这两种格式都需要一个参考时间(2006-01-02 15:04:05)作为参数,使得格式相当容易理解。
You could use the time package's Parse
and Format
to convert it to the desired text format. Both take a reference time (2006-01-02 15:04:05) in the format you require as a parameter which makes the format fairly easy to understand.
dtstr1 := "2010-01-23 11:44:20"
dt,_ := time.Parse("2006-01-02 15:04:05", dtstr1)
dtstr2 := dt.Format("Jan 2 '06 at 15:04")
这篇关于如何在golang中转换日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文