10

我有一个这种格式的日期{Y,M,D}。是否有任何好的支持库或者,我可以使用简单的技巧,比如从这个日期减去三个月,而不会遇到无效日期、闰年等问题。

我最新的类似用途是在 MySql 中,您可以在其中键入:

Select '2011-05-31' - Interval 3 Month;

产生'2011-02-28'。我对如何自己编写这个库不感兴趣,这是我想避免的。

4

3 回答 3

22
1> calendar:gregorian_days_to_date(calendar:date_to_gregorian_days({2011, 7, 14}) - 90).     
{2011,4,15}

http://www.erlang.org/doc/man/calendar.html

于 2011-07-14T16:55:44.963 回答
18

Use edate - specifically the shift function.

> edate:shift({2011,5,31}, -3, months).
{2011,2,28}

Under the hood it uses the calendar module, so it correctly deals with all of the corner cases.

于 2011-07-14T17:02:13.367 回答
3

我为这类事情创建了一个 GitHub gist,其中包含一些有用的实用程序。如果你愿意,可以随意偷走它。

https://gist.github.com/104903

于 2011-07-15T02:58:15.023 回答