Date Formats - Real Quick

polaris · 2017-09-19 04:30:11 · 570 次点击    
这是一个分享于 2017-09-19 04:30:11 的资源,其中的信息可能已经有所发展或是发生改变。

For New Go Programmers.

Key point about formats: the correct component values (ex. 2006 for year, 15 for hour) must be used.

const Format1 = "Jan 2, 2006"
const Format2 = "2006-01-02 15:04:05"
...

var date1 time.Time

input1 := "Apr 1, 2017"

date1, _ = time.Parse(Format1, input1)  // probably want to check for error

output1 := date1.AddDate(1, 1, 1).Format(Format2) // add 1 yr, 1 mo, 1 day & return formatted value

log.Println(output1) // prints 2018-05-02 00:00:00

I know there are many posts about dates, but this simple concept is often covered over with lots of other info.


评论:

metamatic:

It's Go time

jayposs:

Cool

j_d_q:

Godoc time constants lists the possibilities

jayposs:

Definitely the Go to for all the details. Think when you first read it, you miss the point of how easy it is to work with date formats.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

570 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传