-6

Need a C++ function to find out the date of the first day of week from the week number.

Input : year and week number Output : date [It should be 1st day of that week number]

e.g :

  • inputs :

    • year – 2017, week number – 8
      Output: 20th Feb 2017
  • inputs:

    • year – 2017, week number – 10
      Output: `6th March 2017
user2991556
  • 115
  • 1
  • 10
  • 2
    Your wish is unlikely to be fulfilled if you do not show what you have tried so far. Post your code. – mhawke Feb 23 '17 at 11:11
  • is there any standard function that can give me date from week number? otherwise, i will need to take today's date and week and go back/forward to get date i want of week number.. – user2991556 Feb 23 '17 at 11:16
  • There are clues from going the other way here: http://stackoverflow.com/questions/274861/how-do-i-calculate-the-week-number-given-a-date – doctorlove Feb 23 '17 at 11:25
  • No, there is "standard function" that does this. This is what you need to do: take out a piece of paper, and a pen; then write down in short, logical sentences, a step by step process to implement this calculation. Once you have done so, [take this piece of paper to your rubber duck, for a review](https://en.wikipedia.org/wiki/Rubber_duck_debugging). Once your rubber duck approves your proposed algorithm, simply take what you've written down, and translate it directly into C++. You're done. – Sam Varshavchik Feb 23 '17 at 11:54

1 Answers1

0

Using Howard Hinnant's free, open-source, header-only date library, it can look like this:

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

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • Thanks for the reply:) But i am not able to add these files in VS10 - getting error can not open date.h & iso_week.h – user2991556 Feb 24 '17 at 06:46
  • @user2991556: This is a 3rd party library (I'm principle author) located on GitHub. You have to click on the blue-colored link in the first sentence of my answer above. That being said, VS10 is too antiquated to work with this library. It lacks `` altogether, which this library is built upon. This library requires VS13 or later. – Howard Hinnant Feb 24 '17 at 15:45