如何根据日期计算周数?

36
如果我有一个日期,如何计算该年份对应的这个日期是第几周?
例如,在2008年,1月1日至1月6日在第1周,1月7日至13日在第2周,因此如果我的日期是2008年1月10日,我的周数将是2。
可以使用算法来开始编写代码,同时提供示例代码 - 我正在Windows上使用C++开发。

相关内容:

如何从MS SQL Server 2005中获取日期的周数?


现代C++11/14库可以轻松高效地完成这项任务:http://howardhinnant.github.io/iso_week.html - Howard Hinnant
15个回答

37

请注意,虽然你对一年中第n周的定义是可行的,但它并不是“标准”定义。

ISO 8601 定义了日期、时间和时区表示的标准。它定义以星期一为首日的一周,并指出一年的第一周是包含给定年份至少4天的那一周。因此,12月29日、30日和31日可能在第一周(第20xy周,其中 xy = xx + 1),而1月1日、2日和3日则可能在上一年的最后一周(第20xx周)。此外,可能会有第53周存在。

[补充:请注意,C 标准和 `strftime()` 函数提供以星期日或星期一为起始日的周。但不清楚 C 标准是否提供了基于星期日的第0周的年号。有关更多信息,请参见 Emerick Rogul 的答案。]

接下来是有趣的测试阶段——何时才会出现第53周?一个答案是2010年1月1日星期五,它在2009-W53中(实际上,2010年1月3日星期日也是如此)。同样,2005年1月1日(星期六)在2004-W53,但2006年1月1日(星期日)却在2005-W52。

这是从以下 Informix SPL(存储过程语言) 代码的评论中提取的,但很容易阅读——尽管可能没有写作能力。'||' 操作符是 SQL 字符串连接操作符,并且星期天为第0天,星期一为第1天,... 星期六为第6天。注释中有广泛的注释,包括来自标准的相关文本。单行注释以 '--' 开始;可能的多行注释以 '{' 开始,以下一个 '}' 结尾。

-- @(#)$Id: iso8601_weekday.spl,v 1.1 2001/04/03 19:34:43 jleffler Exp $
--
-- Calculate ISO 8601 Week Number for given date
-- Defines procedure: iso8601_weekday().
-- Uses procedure: iso8601_weeknum().

{
According to a summary of the ISO 8601:1988 standard "Data Elements and
Interchange Formats -- Information Interchange -- Representation of
dates and times":

    The week notation can also be extended by a number indicating the
    day of the week.  For example the day 1996-12-31 which is the
    Tuesday (day 2) of the first week of 1997 can also be written as

        1997-W01-2 or 1997W012

    for applications like industrial planning where many things like
    shift rotations are organized per week and knowing the week number
    and the day of the week is more handy than knowing the day of the
    month.

This procedure uses iso8601_weeknum() to format the YYYY-Www part of the
date, and appends '-d' to the result, allowing for Informix's coding of
Sunday as day 0 rather than day 7 as required by ISO 8601.
}

CREATE PROCEDURE iso8601_weekday(dateval DATE DEFAULT TODAY) RETURNING CHAR(10);
    DEFINE rv CHAR(10);
    DEFINE dw CHAR(4);
    LET dw = WEEKDAY(dateval);
    IF dw = 0 THEN
            LET dw = 7;
    END IF;
    RETURN iso8601_weeknum(dateval) || '-' || dw;
END PROCEDURE;
-- @(#)$Id: iso8601_weeknum.spl,v 1.1 2001/02/27 20:36:25 jleffler Exp $
--
-- Calculate ISO 8601 Week Number for given date
-- Defines procedures: day_one_week_one() and iso8601_weeknum().

{
According to a summary of the ISO 8601:1988 standard "Data Elements and
Interchange Formats -- Information Interchange -- Representation of
dates and times":

    In commercial and industrial applications (delivery times,
    production plans, etc.), especially in Europe, it is often required
    to refer to a week of a year.  Week 01 of a year is per definition
    the first week which has the Thursday in this year, which is
    equivalent to the week which contains the fourth day of January.  In
    other words, the first week of a new year is the week which has the
    majority of its days in the new year.  Week 01 might also contain
    days from the previous year and the week before week 01 of a year is
    the last week (52 or 53) of the previous year even if it contains
    days from the new year.  A week starts with Monday (day 1) and ends
    with Sunday (day 7).  For example, the first week of the year 1997
    lasts from 1996-12-30 to 1997-01-05 and can be written in standard
    notation as

        1997-W01 or 1997W01

    The week notation can also be extended by a number indicating the
    day of the week.  For example the day 1996-12-31 which is the
    Tuesday (day 2) of the first week of 1997 can also be written as

        1997-W01-2 or 1997W012

    for applications like industrial planning where many things like
    shift rotations are organized per week and knowing the week number
    and the day of the week is more handy than knowing the day of the
    month.

Referring to the standard itself, section 3.17 defines a calendar week:

    week, calendar: A seven day period within a calendar year, starting
    on a Monday and identified by its ordinal number within the year;
    the first calendar week of the year is the one that includes the
    first Thursday of that year.  In the Gregorian calendar, this is
    equivalent to the week which includes 4 January.

Section 5.2.3 "Date identified by Calendar week and day numbers" states:

    Calendar week is represented by two numeric digits.  The first
    calendar week of a year shall be identified as 01 [...]

    Day of the week is represented by one decimal digit.  Monday
    shall be identified as day 1 of any calendar week [...]

Section 5.2.3.1 "Complete representation" states:

    When the application clearly identifies the need for a complete
    representation of a date identified by calendar week and day
    numbers, it shall be one of the alphanumeric representations as
    follows, where CCYY represents a calendar year, W is the week
    designator, ww represents the ordinal number of a calendar week
    within the year, and D represents the ordinal number within the
    calendar week.

    Basic format: CCYYWwwD
        Example: 1985W155
    Extended format: CCYY-Www-D
        Example: 1985-W15-5

Both the summary and the formal definition are intuitively clear, but it
is not obvious how to translate it into an algorithm.  However, we can
deal with the problem by exhaustively enumerating the seven options for
the day of the week on which 1st January falls (with actual year values
for concreteness):

    1st January 2001 is Monday    => Week 1 starts on 2001-01-01
    1st January 2002 is Tuesday   => Week 1 starts on 2001-12-31
    1st January 2003 is Wednesday => Week 1 starts on 2002-12-30
    1st January 2004 is Thursday  => Week 1 starts on 2003-12-29
    1st January 2010 is Friday    => Week 1 starts on 2010-01-04
    1st January 2005 is Saturday  => Week 1 starts on 2005-01-03
    1st January 2006 is Sunday    => Week 1 starts on 2006-01-02

(Cross-check: 1st January 1997 was a Wednesday; the summary notes state
that week 1 of 1997 started on 1996-12-30, which is consistent with the
table derived for dates in the first decade of the third millennium
above).

When working with the Informix DATE types, bear in mind that Informix
uses WEEKDAY values 0 = Sunday, 1 = Monday, 6 = Saturday.  When the
weekday of the first of January has the value in the LH column, you need
to add the value in the RH column to the 1st of January to obtain the
date of the first day of the first week of the year.

    Weekday         Offset to
    1st January     1st day of week 1

    0               +1
    1                0
    2               -1
    3               -2
    4               -3
    5               +3
    6               +2

This can be written as MOD(11-w,7)-3 where w is the (Informix encoding
of the) weekday of 1st January and the value 11 is used to ensure that
no negative values are presented to the MOD operator.  Hence, the
expression for the date corresponding to the 1st day (Monday) of the 1st
week of a given year, yyyy, is:

    d1w1 = MDY(1, 1, yyyy) + MOD(11 - WEEKDAY(MDY(1,1,yyyy)), 7) - 3

This expression is encapsulated in stored procedure day_one_week_one:
}

CREATE PROCEDURE day_one_week_one(yyyy INTEGER) RETURNING DATE;
    DEFINE jan1 DATE;
    LET jan1 = MDY(1, 1, yyyy);
    RETURN jan1 + MOD(11 - WEEKDAY(jan1), 7) - 3;
END PROCEDURE;

{
Given this date d1w1, we can calculate the week number of any other date
in the same year as:

    TRUNC((dateval - d1w1) / 7) + 1

The residual issues are ensuring that the wraparounds are correct.  If
the given date is earlier than the start of the first week of the year
that contains it, then the date belongs to the last week of the previous
year.  If the given date is on or after the start of the first week of
the next year, then the date belongs to the first week of the next year.

Given these observations, we can write iso8601_weeknum as shown below.
(Beware: iso8601_week_number() is too long for servers with the
18-character limit; so is day_one_of_week_one()).

Then comes the interesting testing phase -- when do you get week 53?
One answer is on Friday 1st January 2010, which is in 2009-W53 (as,
indeed, is Sunday 3rd January 2010).  Similarly, Saturday 1st January
2005 is in 2004-W53, but Sunday 1st January 2006 is in 2005-W52.
}

CREATE PROCEDURE iso8601_weeknum(dateval DATE DEFAULT TODAY) RETURNING CHAR(8);
    DEFINE rv CHAR(8);
    DEFINE yyyy CHAR(4);
    DEFINE ww CHAR(2);
    DEFINE d1w1 DATE;
    DEFINE tv DATE;
    DEFINE wn INTEGER;
    DEFINE yn INTEGER;
    -- Calculate year and week number.
    LET yn = YEAR(dateval);
    LET d1w1 = day_one_week_one(yn);
    IF dateval < d1w1 THEN
        -- Date is in early January and is in last week of prior year
        LET yn = yn - 1;
        LET d1w1 = day_one_week_one(yn);
    ELSE
        LET tv = day_one_week_one(yn + 1);
        IF dateval >= tv THEN
            -- Date is in late December and is in the first week of next year
            LET yn = yn + 1;
            LET d1w1 = tv;
        END IF;
    END IF;
    LET wn = TRUNC((dateval - d1w1) / 7) + 1;
    -- Calculation complete: yn is year number and wn is week number.
    -- Format result.
    LET yyyy = yn;
    IF wn < 10 THEN
        LET ww = '0' || wn;
    ELSE
        LET ww = wn;
    END IF
    LET rv = yyyy || '-W' || ww;
    RETURN rv;
END PROCEDURE;
为了完整起见,反函数也可以使用上面的day_one_week_one()函数很容易地编写:

-- @(#)$Id: ywd_date.spl,v 1.1 2012/12/29 05:13:27 jleffler Exp $
-- @(#)Create ywd_date() and ywdstr_date() stored procedures

-- Convert a date in format year, week, day (ISO 8601) to DATE.
-- Two variants:
-- ywd_date(yyyy SMALLINT, ww SMALLINT, dd SMALLINT) RETURNING DATE;
-- ywdstr_date(ywd CHAR(10)) RETURNING DATE;

-- NB: If week 53 is supplied, there is no check that the year had week
--     53 (GIGO).
-- NB: If year yyyy is a leap year and yyyy-01-01 falls on Wed (3) or
--     Thu (4), there are 53 weeks in the year.
-- NB: If year yyyy is not a leap year and yyyy-01-01 falls on Thu (4),
--     there are 53 weeks in the year.

CREATE PROCEDURE ywd_date(yyyy SMALLINT, ww SMALLINT, dd SMALLINT) RETURNING DATE AS date;
    DEFINE d DATE;
    -- Check ranges
    IF yyyy < 1 OR yyyy > 9999 OR ww < 1 OR ww > 53 OR dd < 1 OR dd > 7 THEN
        RETURN NULL;
    END IF;
    LET d = day_one_week_one(yyyy);
    LET d = d + (ww - 1) * 7 + (dd - 1);
    RETURN d;
END PROCEDURE;

-- Input: 2012-W52-5
CREATE PROCEDURE ywdstr_date(ywd CHAR(10)) RETURNING DATE AS date;
    DEFINE yyyy SMALLINT;
    DEFINE ww   SMALLINT;
    DEFINE dd   SMALLINT;
    LET yyyy = SUBSTR(ywd,  1, 4);
    LET ww   = SUBSTR(ywd,  7, 2);
    LET dd   = SUBSTR(ywd, 10, 1);
    RETURN ywd_date(yyyy, ww, dd);
END PROCEDURE;

CREATE TEMP TABLE test_dates(d DATE);
INSERT INTO test_dates VALUES('2011-12-28');
INSERT INTO test_dates VALUES('2011-12-29');
INSERT INTO test_dates VALUES('2011-12-30');
INSERT INTO test_dates VALUES('2011-12-31');
INSERT INTO test_dates VALUES('2012-01-01');
INSERT INTO test_dates VALUES('2012-01-02');
INSERT INTO test_dates VALUES('2012-01-03');
INSERT INTO test_dates VALUES('2012-01-04');
INSERT INTO test_dates VALUES('2012-01-05');
INSERT INTO test_dates VALUES('2012-01-06');
INSERT INTO test_dates VALUES('2012-01-07');

SELECT d, iso8601_weeknum(d), iso8601_weekday(d), ywdstr_date(iso8601_weekday(d))
  FROM test_dates
 ORDER BY d;

如评论所述,该代码即使应该只接受52周的年份,也会接受第53周的日期。


哦,当前标准的版本是ISO/IEC 8601:2004。 - Jonathan Leffler
非常感谢您提供如此详细的解释,我之前并没有意识到计算周数有不同的含义和方法! - Big GH
哇塞!我想知道有多少人知道这个4天的事情...谢谢! - Olie

23
伪代码:
int julian = getDayOfYear(myDate)  // Jan 1 = 1, Jan 2 = 2, etc...
int dow = getDayOfWeek(myDate)     // Sun = 0, Mon = 1, etc...
int dowJan1 = getDayOfWeek("1/1/" + thisYear)   // find out first of year's day
// int badWeekNum = (julian / 7) + 1  // Get our week# (wrong!  Don't use this)
int weekNum = ((julian + 6) / 7)   // probably better.  CHECK THIS LINE. (See comments.)
if (dow < dowJan1)                 // adjust for being after Saturday of week #1
    ++weekNum;
return (weekNum)

为了澄清,这个算法假设你的周数是按照以下方式编号的:
S  M  T  W  R  F  S
            1  2  3    <-- week #1
4  5  6  7  8  9 10    <-- week #2
[etc.]

在大多数语言中,getDayOfWeek()和getDayOfYear()都是标准的日期操作。如果您的语言中没有这些函数,您可以从某个已知的日期(如1970年1月1日)开始向前计数,并查看当时为一周的哪一天。

如果您要实现自己的日期计数程序,请记住,能够被100整除的年份不是闰年,除非它们同时能够被400整除。因此,1900年不是闰年,但2000年是闰年。如果您要追溯很长时间,您需要处理格里高利历和儒略历之间的问题等,请参考维基百科获取更多信息。

这个链接详细介绍了Windows/C++中的日期/时间函数。


1
除非我弄错了,否则这是不正确的。只需从示例中取出1月7日:julian = 7,dow = 3,dowJan1 = 4,weekNum =(7/7)+1 = 2。然后dow小于dowJan1,因此weekNum + 1 = 3。但很明显1月7日是第2周。 - NKijak
即使使用 ((julian / 7) + 1),结果仍然不正确:例如对于2012年4月5日,它返回16而不是15。 - phortx
@phortx:请记住,这个例子最初是在浏览器中输入的伪代码,并逐渐演变成几乎可以工作的C代码,其中一些部分可能不太正确,需要调整。特别是,您指定的行是“badWeekNum”,而其下面的行标记为“可能更好”。如果您有修复方法,我将很乐意编辑示例以进一步改进它。谢谢! - Olie
抱歉,我的意思是((julian + 6) / 7)...但两者都不能在所有边缘情况下正常工作。我知道这只是一个快速而简单的示例,但如果你在谷歌上搜索算法,这个答案是最好的结果之一。我尝试了许多在网上找到的算法,但没有一个能在所有边缘情况下正常工作。很难找到一个可行的周数算法。 - phortx
1
对于您的例子,2012年4月5日:4月5日是一年中的第96天(31 + 29 + 31 = 91 - 这是1月、2月和3月 - 加上5 = 96)(96 + 6) / 7 == (102 / 7) == 14。2012年4月5日确实是2012年第14周的一部分。我相信算法是正确的。你有失败的例子吗?(不要忘记你必须使用整数!) - Olie
显示剩余2条评论

8

我强烈建议使用C标准库的时间函数来计算周数。具体来说,strftime函数有指定符可以在给定以打破(struct tm)格式表示的日期时打印周数(以及许多其他值)。以下是一个小样例程序,说明了这一点:

#include <stdio.h>
#include <string.h>
#include <time.h>

int
main(void)
{
  struct tm tm;
  char timebuf[64];

  // Zero out struct tm
  memset(&tm, 0, sizeof tm);

  // November 4, 2008 11:00 pm
  tm.tm_sec = 0;
  tm.tm_min = 0;
  tm.tm_hour = 23;
  tm.tm_mday = 4;
  tm.tm_mon = 10;
  tm.tm_year = 108;
  tm.tm_isdst = -1;

  // Call mktime to recompute tm.tm_wday and tm.tm_yday
  mktime(&tm);

  if (strftime(timebuf, sizeof timebuf, "%W", &tm) != 0) {
    printf("Week number is: %s\n", timebuf);
  }

  return 0;
}

这个程序的输出结果(在Linux上使用GCC编译,在Windows上使用Microsoft Visual Studio 2005 SP1编译)是:
Week number is: 44
你可以在这里了解更多关于strftime的信息:这里

请注意,还有%U,%G,%u,%V等等。 - Jonathan Leffler
ISO C 90不支持%U、%G、%u、%V,Visual Studio 2005(我相信还包括2008)在这方面遵循C 90标准,因此在Windows上这些内容是不可用的。它们在http://www.opengroup.org/onlinepubs/009695399/functions/strftime.html有记录。 - danio
5
查看strftime的源代码,这是所使用的计算公式:(t->tm_yday + 7 - (t->tm_wday ? (t->tm_wday - 1) : 6)) / 7。直接使用这个计算公式可以避免需要将日期转换成/从字符串。 - Kekoa

3

抱歉,我是新来的,无法在答案本身上发表评论,但是带有勾号的答案中的伪代码并不完全正确。

伪代码:

int julian = getDayOfYear(myDate)  // Jan 1 = 1, Jan 2 = 2, etc...
int dow = getDayOfWeek(myDate)     // Sun = 0, Mon = 1, etc...
int dowJan1 = getDayOfWeek("1/1/" + thisYear)   // find out first of year's day
int weekNum = (julian / 7) + 1     // Get our week#
if (dow < dowJan1)                 // adjust for being after Saturday of week #1
    ++weekNum;
return (weekNum)

你不应该寻找“第一天”,而是应该寻找去年的最后一天。

getDayOfWeek("12/31/" + thisYear-1)

would be correct

更正确。
getDayOfWeek("1/1/" + thisYear) 

如果你不这样做,去年最后一个工作日(比如星期一)将永远提前一周。


2

struct tm用于表示"分解时间",至少具有以下字段:

int    tm_sec   秒 [0,60]。 
int    tm_min   分钟 [0,59]。 
int    tm_hour  小时 [0,23]。 
int    tm_mday  一个月的日期 [1,31]。 
int    tm_mon   一年中的月份 [0,11]。 
int    tm_year  自1900年以来的年数。 
int    tm_wday  一周的日期 [0,6] (星期日=0)。 
int    tm_yday  一年的日期 [0,365]。 
int    tm_isdst 夏令时标志。 

您可以使用localtime()函数从time_t创建一个struct tm。

您可以使用mktime()函数从struct tm创建一个time_t。

struct tm最好的部分是您可以执行诸如将年份加24之类的操作,并在调用mktime()时获得一个2年后的time_t(这适用于其任何成员,因此您可以例如将小时增加1000,然后获得一个41天后的time_t)...


2
但是struct tm没有年周数,所以在这种情况下它的使用受到限制。 - danio

2
使用gmtime或localtime计算自周日以来的天数(即星期几)和自1月1日以来的天数(请注意,后者中的Jan 1为“0”)。
任意的是决定第一周从哪一年的哪一天开始:通常只取决于1月1日是星期几,这当然可以从gmtime的两个信息中计算出来。然后使用一个表来查找7种可能性,这可能比编写规则更容易。
例如,我认为Outlook使用标准,即第1周是包含星期四的第1周。因此,如果1月1日是星期日,则第1周的第1天是1月1日,或第0天。其余的可能性是星期一,-1;星期二,-2;星期三,-3;星期四,-4;星期五,2;星期六,1。
请注意负数:“第1周的星期日”实际上在7种情况中有4种不存在,但是如果我们假装它是前一年的一天,我们将得到正确的答案。
一旦你有了这个,它与你的日期之间的天数告诉你周数:除以7并加1。
话虽如此,我想象中肯定有一个Windows API会给你与Outlook使用相同的周数。我只是不知道它是什么,当然,如果你的第1周规则与Outlook的不同,那么它可能没什么用。
未经测试的代码:
int firstdays[7] = { 0, -1, -2, -3, -4, 2, 1 }; // or some other Week 1 rule
struct tm breakdown;
time_t target = time_you_care_about();
_gmtime_s(&breakdown,&target);
int dayofweek = breakdown.tm_wday;
int dayofyear = breakdown.tm_yday;

int jan1wday = (dayofweek - dayofyear) % 7;
if (jan1wday < 0) jan1wday += 7;

int week1first = firstdays[jan1wday];
if (dayofyear < week1first) return 0;
return ((dayofyear - week1first)/7) + 1;

无论如何,类似于这样的东西。


2
public int GetWeekOfYear(DateTime todayDate)
{
    int days = todayDate.DayOfYear;
    float result = days / 7;
    result=result+1;
    Response.Write(result.ToString());
    return Convert.ToInt32(result);
}

只需将当前日期作为参数传递给此函数。然后,您将获得当前的周数。希望这可以解决您的问题。欢迎提出任何建议。

2
/**********************************************************************************
Function Name: rtcCalcYearWeek
Description  : Function to calculate the working week of the year (changing on a Monday)
Arguments    : IN  iYear - The year 2000...
               IN  iMonth - The month 1..12
               IN  iDay - The day 1..31
               IN  iWeekDay - The week day 0 = Monday ... 6 = Sunday
Return Value : The year week 1..52
***********************************************************************************/
int rtcCalcYearWeek(int iYear, int iMonth, int iDay, int iWeekDay)
{
    int iLeap = 0;
    static const int ppiYearDays[2][13] =
    {
         /* Normal year */
         {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
         /* Leap year */
         {0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
    };
    /* Check for leap year */
    if (((iYear % 4) == 0) && (((iYear % 100) != 0) || ((iYear % 400) == 0)))
    {
        iLeap = 1;
    }
    /* Calculate the year week */
    return (((ppiYearDays[iLeap][iMonth] + iDay) - (iWeekDay + 7) % 7 + 7) / 7) + 1;
}
/***********************************************************************************
End of function  rtcCalcYearWeek
***********************************************************************************/

/**********************************************************************************
* Function Name: rtcCalcWeekDay
* Description  : Function to calculate the week day for a given date from 2000
*                to 2099.
* Arguments    : IN  iDay - The day 1..31
*                IN  iMonth - The month 1..12
*                IN  iYear - The year 2000..2099
* Return Value : The weekday 0 = Monday ... 6 = Sunday
***********************************************************************************/
int rtcCalcWeekDay(int iDay, int iMonth, int iYear)
{
    if (iMonth < 3)
    {
        iMonth += 12;
        iYear -= 1;
    }
    return (iDay + (2 * iMonth) + (6 * (iMonth + 1) / 10) + iYear 
            + (iYear / 4)- (iYear / 100) + (iYear / 400)) % 7;
}
/***********************************************************************************
End of function  rtcCalcWeekDay
***********************************************************************************/

1

我的定义不符合ISO 8601标准(但对于我的目的来说足够好且快速):

// week number of the year
// (Monday as the first day of the week) as a decimal number [00,53].
// All days in a new year preceding the first Monday are considered to be in week 0.
int GetWeek(const struct tm& ts)
{
    return (ts.tm_yday + 7 - (ts.tm_wday ? (ts.tm_wday - 1) : 6)) / 7;
}

1
使用来自howardhinnant.github.io/iso_week.htmliso_week.h,这很容易实现:
#include <iostream>
#include "iso_week.h"

int main() {
    using namespace iso_week;
    using namespace std::chrono;
    // Get the current time_point and floor to convert to the sys_days:
    auto today = floor<days>(system_clock::now());
    // Convert from sys_days to iso_week::year_weeknum_weekday format
    auto yww = year_weeknum_weekday{today};
    // Print current week number of the year
    std::cout << "The current week of " << yww.year() << " is: " 
              << yww.weeknum() << std::endl;

    // Set any day
    auto any_day = 2014_y/9/28;
    // Get week of `any_day`
    std::cout << "The week of " << any_day.year() << " on `any day` was: " 
              << any_day.weeknum() << std::endl;   
}

并且输出是:

The current week of 2019 is: W18
The week in 2014 on `any day` was: W09

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接