如何将日期转换为不同的格式? [英] How to convert date to different formats?

查看:38
本文介绍了如何将日期转换为不同的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Go 中将日期格式从 2010-01-23 11:44:20 转换为 10 年 1 月 23 日 11:44.我尝试了 time 包中的一些功能,但无法实现.

I would like to convert date format from 2010-01-23 11:44:20 to Jan 23 '10 at 11:44 in Go. I tried few functions from time package but couldn't make it.

有人可以帮我解决这个问题吗?

Could someone help me with this?

推荐答案

你可以使用时间包的 <代码>解析Format 将其转换为所需的文本格式.两者都采用您需要的格式的参考时间 (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")

测试的游乐场.

这篇关于如何将日期转换为不同的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆