title: C++ - compare two dates
body: <p>I am writing a application which needs the possibility to compare two dates. This is what I have so far:</p>
  
  <pre><code>struct entry {
      string text;
      string date; // format: dd.mm.yyyy
      bool finished; 
  };
  
  string addNulls(int number, int cols) {
      string num = to_string(number);
      if (num.size() &lt; cols) {
          int count = cols - num.size();
          for (int i = 0; i &lt; count; i++) {
              num = "0" + num;
          }
      }
      return num;
  }
  
  // [...]
  
  entry e = {"here is some text", "21.03.2019", false};
  
  int day2 = atoi(e.date.substr(0, 2).c_str());
  int month2 = atoi(e.date.substr(3, 2).c_str());
  int year2 = atoi(e.date.substr(6, 4).c_str());
  
  time_t t = time(0);
  struct tm * now = localtime(&amp;t);
  
  string date1 = e.date.substr(6, 4) + "-" + e.date.substr(3, 2) + "-" + e.date.substr(0, 2) + " 00:00:00";
  string date2 = addNulls(now-&gt;tm_year, 4) + "-" + addNulls(now-&gt;tm_mon, 2) + "-" + addNulls(now-&gt;tm_mday, 2) + " 00:00:00";
  
  if(date2 &gt; date1) {
      // do something
  }
  </code></pre>
  
  <p>the code gets an "entry" struct which contains a date. Than the code compares the date with the actual time. The problem is, it does not work! I run some tests with some example content, but the result (date2 > date1) returns false.</p>
  
  <p>Why?</p>
  
  <p>I read this: <a href="https://stackoverflow.com/questions/8406703/c-compare-to-string-dates">C++ compare to string dates</a></p>