Golang:到目前为止的字符串日期 [英] Golang: String date to date

查看:101
本文介绍了Golang:到目前为止的字符串日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串日期格式转换为日期,我的日期字符串格式为

How to convert string date format to date, I have date string in format of

YYYY-DD-MM 

以下我尝试没有运气。

Following I tried with no luck.

t, err := time.Parse("%Y-%m-%d", "2011-01-19")
t, err := time.Parse("YYYY-MM-DD", "2011-01-19")
t, err := time.Parse("2016-01-20", "2011-01-19")

所有上述语句都会给出解析错误。

all above statements are giving parse errors.

推荐答案

请阅读 time.Parse


布局通过显示参考时间如何定义格式将被

The layout defines the format by showing how the reference time, defined to be

Mon Jan 2 15:04:05 -0700 MST 2006

Mon Jan 2 15:04:05 -0700 MST 2006

将被解释为是否价值;它作为输入格式的一个例子。那么对输入字符串的解释也是一样的。

would be interpreted if it were the value; it serves as an example of the input format. The same interpretation will then be made to the input string.

所以正确的格式是

t, err := time.Parse("2006-01-02", "2011-01-19")

这篇关于Golang:到目前为止的字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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