简体   繁体   English

如何从C++中的周数中找出一周的第一天的日期

[英]How to find out the date of the first day of week from the week number in C++

Need a C++ function to find out the date of the first day of week from the week number.需要一个 C++ 函数来从周数中找出一周的第一天的日期。

Input : year and week number Output : date [It should be 1st day of that week number]输入:年和周数输出:日期[应该是该周数的第一天]

eg :例如:

  • inputs :输入:

    • year – 2017 , week number – 8年 – 2017 ,周数 – 8
      Output: 20th Feb 2017输出: 20th Feb 2017
  • inputs:输入:

    • year – 2017 , week number – 10年 – 2017 ,周数 – 10
      Output: `6th March 2017输出:`2017 年 3 月 6 日

Using Howard Hinnant's free, open-source, header-only date library , it can look like this:使用Howard Hinnant 的免费、开源、仅标头日期库,它看起来像这样:

#include "date.h"
#include "iso_week.h"
#include <iostream>

int
main()
{
    using namespace iso_week::literals;
    std::cout << date::year_month_day{2017_y/8_w/mon} << '\n';
    std::cout << date::year_month_day{2017_y/10_w/mon} << '\n';
}

which outputs:输出:

2017-02-20
2017-03-06

There are also getters for year, month and day on the year_month_day types, and plenty of formatting options.year_month_day类型上还有用于年、月和日的year_month_day ,以及大量的格式选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM