-2

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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Laith
  • 9
  • 3
    Please don't spam tags. Choose one standard version that you are using. And is `c++-cli` even relevant? – Yksisarvinen Apr 04 '22 at 20:16
  • https://github.com/HowardHinnant/date or the c++20 equivalent will be helpful – Alan Birtles Apr 04 '22 at 20:20
  • i'm sorry cause this is my first question here no it's not relevant – Laith Apr 04 '22 at 20:27
  • Although not exactly the same as your question, this question may also offer assistance if you aren't using C++20, and don;t want to use a third party library. https://stackoverflow.com/questions/35009876/c-get-dates-between-two-dates?rq=1 – Cascades Apr 04 '22 at 20:34
  • @Laith what version of C++ are you seeking an answer for? – Drew Dormann Apr 04 '22 at 22:19
  • @DrewDormann anyversion doesn't matter but i need it to work with codeforces compiler not adding library or something like that – Laith Apr 04 '22 at 22:26
  • 3
    @Laith *anyversion doesn't matter* -- It does matter, why? Because of what you are saying here: -- *i need it to work with codeforces compiler* -- What is "codeforces compiler"? You need to be aware of the tools you are using. There are 3 major compiler vendors -- Microsoft (Visual C++), g++, and clang. If you cannot identify your "codeforces compiler" as one of those 3, then you need to do more research on the tools you're using, or better yet, get your own local compiler, install it, and you have no issues. – PaulMcKenzie Apr 04 '22 at 22:53

1 Answers1

3

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.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • i'm sorry but are you sure from the date library cause I try it on many compilers such as codeforces compiler it didn't work , I need anyway can work on codeforces compiler and for c++ language it doesn't matter the version of c++ and thanks for your collaboration – Laith Apr 04 '22 at 22:18
  • i'm sorry i just noticed that you are the creator of this library , I need a way that could wok compiler such as codeforces not by adding additional libraries or something ,is there like a good way to do that ,it doesn't matter the version of c++ – Laith Apr 04 '22 at 22:29
  • I don't have the codeforces environment to experiment with. If you can post the very first error message you are getting, I can try to diagnose the problem. – Howard Hinnant Apr 04 '22 at 23:05