'How to get all the dates between two dates in c++ standard library?
how can i get all dates between two dates in c++ .The input format is like that 2022-01-14"
i need to get for example dates between "2022-05-13" and "2021-12-12". i need all dates between this two in easier way cause it's really hard if I will compare the months,days and years . and I want to store them in a vector<string> to deal with them. i try alot with the library "ctime.h" but i didn't know how to work with it.
Solution 1:[1]
Unfortunately your post was edited in such a way as to hide the fact that you are not interested in a C++20 solution. The reason that this is relevant is that C++20 has a very elegant and easy to use solution. And, for pre-C++20, there exists a free, open-source, header-only preview of this part of C++20 which will work with C++11/14/17.
Here is what the solution looks like with this preview:
#include "date/date.h"
#include <iostream>
#include <vector>
int
main()
{
using namespace date;
using namespace std;
vector<sys_days> v;
for (sys_days d = 2021_y/12/12; d <= sys_days{2022_y/05/13}; d += days{1})
v.push_back(d);
for (auto d : v)
cout << d << '\n';
}
Output:
2021-12-12
2021-12-13
2021-12-14
...
2022-05-11
2022-05-12
2022-05-13
sys_days is a std::chrono::time_point based on system_clock but with a precision of days. Under the hood this is nothing more than a count of days since 1970-01-01. And there exists easy conversions between this date data structure and a {year, month, day} data structure. And there are nice printing facilities.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Howard Hinnant |
