请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

为什么解析时间字符串格式是这样?

我看到我们的代码有这样使用parse的format

const baseFormat = "2006-01-02 15:04:05"

老师说这是golang中的特殊时间点,为什么呢?
我看到golang的文档上是这样写的

// Some valid layouts are invalid time values, due to format specifiers
// such as _ for space padding and Z for zone information.
// For example the RFC3339 layout 2006-01-02T15:04:05Z07:00
// contains both Z and a time zone offset in order to handle both valid options:
// 2006-01-02T15:04:05Z
// 2006-01-02T15:04:05+07:00
t, _ = time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")

这个时间有什么特殊的意义吗?所有的时间format都不想用这个时间来作为format吗?
很多语言用“yyyy-mm-d HH:MM:SS”来作为format。

正在回答

1回答

同学你好,这个时间格式化的方式,初衷应该是作者为了方便大家记忆吧。可以看下这段解释:

// The layout string used by the Parse function and Format method
// shows by example how the reference time should be represented.
// We stress that one must show how the reference time is formatted,
// not a time of the user's choosing. Thus each layout string is a
// representation of the time stamp,
//	Jan 2 15:04:05 2006 MST
// An easy way to remember this value is that it holds, when presented
// in this order, the values (lined up with the elements above):
//	  1 2  3  4  5    6  -7
// There are some wrinkles illustrated below.

简单就是1月2号3点4分5秒2006年-7时区,这种1234567的方式对于熟悉英语写法的人来说会比较容易理解和记忆吧。而对于使用惯了yyyy-mm-dd或YYYY-MM-dd这种方式来做日期格式化的开发者来说,的确是有点不太好接受。

0 回复 有任何疑惑可以回复我~
  • 提问者 拧壶冲 #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2020-10-20 16:48:07
  • 提问者 拧壶冲 #2
    那就是说这个format,也可以写成这样?
    const baseFormat = "2006年01月02日 15时04分05秒"
    或者
    const baseFormat = "2006/01/02 15-04-05"
    回复 有任何疑惑可以回复我~ 2020-10-20 17:36:48
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号