> Erlang中文手册 > iso_week_number/1 获取当前指定时间日期的周数

calendar:iso_week_number/1

获取当前指定时间日期的周数

用法:

iso_week_number(Date) -> yearweeknum()

内部实现:

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

返回一个 {年, 周数} 格式的元组,其表示指定日期时间 Date 的年份和周数。

calendar:iso_week_number({2011, 11, 11}).
{Date, _Time} = calendar:local_time(),
calendar:iso_week_number(Date).