英文:
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.Format或metakeule/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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论