> Erlang中文手册 > last_day_of_the_month/2 计算在一个月中的天数

calendar:last_day_of_the_month/2

计算在一个月中的天数

用法:

last_day_of_the_month(Year, Month) -> LastDay

内部实现:

%% last_day_of_the_month(Year, Month)
%%
%% Returns the number of days in a month.
%%
-spec last_day_of_the_month(Year, Month) -> LastDay when
      Year :: year(),
      Month :: month(),
      LastDay :: lDOM().
last_day_of_the_month(Y, M) when is_integer(Y), Y >= 0 ->
    last_day_of_the_month1(Y, M).

-spec last_day_of_the_month1(year(),month()) -> ldom().
last_day_of_the_month1(_, 4) -> 30;
last_day_of_the_month1(_, 6) -> 30;
last_day_of_the_month1(_, 9) -> 30;
last_day_of_the_month1(_,11) -> 30;
last_day_of_the_month1(Y, 2) ->
   case is_leap_year(Y) of
      true -> 29;
      _    -> 28
   end;
last_day_of_the_month1(_, M) when is_integer(M), M > 0, M 
    31.

这个函数返回在指定某个月中的该月天数。

Now = erlang:now(),
{{Year, Month, _Day}, _Time} = calendar:now_to_local_time(Now),
calendar:last_day_of_the_month(Year, Month).
Now = erlang:now(),
calendar:last_day_of_the_month(2014, 7).