Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date from human readable format

Tags:

datetime

go

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".

like image 799
RuslanN Avatar asked Sep 26 '22 05:09

RuslanN


1 Answers

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.

like image 176
VonC Avatar answered Sep 28 '22 07:09

VonC