将日期从人类可读格式转换为计算机可处理的格式。

huangapple go评论76阅读模式
英文:

Convert date from human readable format

问题

我知道有一个用于执行相反操作的库。我需要将字符串格式化,例如将“a year ago”格式化为任何日期格式“29/02/15”。

英文:

I know that there is a library for doing opposite. I need to format string, for example, "a year ago" to any date format "29/02/15".

答案1

得分: 4

这很棘手,因为那些库(比如timeago)也会将时间翻译成“不到一分钟前”(这是无法再翻译回具体时间的)。

对于确切的表达(比如“3个月前”),你需要使用time.AddDate()(这里是t.AddDate(0, -3, 0).Month())。

然后,你可以使用Time.Formatmetakeule/fmtdate将得到的时间格式化为任何日期格式。

英文:

This is tricky because those libraries (like timeago) would also translate time in "less than a minute ago" (which you cannot translate back into an exact time)

For exact expressions (like "3 months ago"), you need to use time.AddDate() (here t.AddDate(0, -3, 0).Month())

Then you can format the resulting time into any date format you want with Time.Format or metakeule/fmtdate.

huangapple
  • 本文由 发表于 2016年3月1日 00:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/35705146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定