> Erlang中文手册 > iso_week_number/0 获取当前本地时间日期的周数

calendar:iso_week_number/0

获取当前本地时间日期的周数

用法:

iso_week_number() -> yearweeknum()

内部实现:

%%
%% Calculates the iso week number for the current date.
%%
-spec iso_week_number() -> yearweeknum().
iso_week_number() ->
    {Date, _} = local_time(),
    iso_week_number(Date).


%%
%% Calculates the iso week number for the given date.
%%
-spec iso_week_number(Date) -> yearweeknum() when
      Date :: date().
iso_week_number({Year, Month, Day}) ->
    D = date_to_gregorian_days({Year, Month, Day}),
    W01_1_Year = gregorian_days_of_iso_w01_1(Year),
    W01_1_NextYear = gregorian_days_of_iso_w01_1(Year + 1),
    if W01_1_Year =
	    % Current Year Week 01..52(,53)
	    {Year, (D - W01_1_Year) div 7 + 1};
	D 
	    % Previous Year 52 or 53
	    PWN = case day_of_the_week(Year - 1, 1, 1) of
		4 -> 53;
		_ -> case day_of_the_week(Year - 1, 12, 31) of
			4 -> 53;
			_ -> 52
		     end
		end,
	    {Year - 1, PWN};
	W01_1_NextYear =
	    % Next Year, Week 01
	    {Year + 1, 1}
    end.

返回一个 {年, 周数} 格式的元组,其表示当前实际日期时间的年份和周数。实际日期时间的值是使用 calendar:local_time/0 来获取。

calendar:iso_week_number().