0

I have just started learning C++ and I want to try to work with dates now. How can I calculate number of days between two dates? I tried using TimeSpan and DateTime classes, but I can't get them to work..

I found this on other thread and something as simple as this would be great:

DateTime xmas = new DateTime(2009, 12, 25);
double daysUntilChristmas = xmas.Subtract(DateTime.Today).TotalDays;
Artem Kulikov
  • 2,250
  • 19
  • 32
  • 4
    It does not work because that code is not C++. – Banex Aug 02 '15 at 18:30
  • If you use CLR mode, it would be nice to say it. If you do not use is, please also say it. The example uses .Net classes, to you want to use them, or do you prefere to use the standard C++ library ? – Serge Ballesta Aug 02 '15 at 18:31
  • @SergeBallesta It is not C++ because otherwise it would have read `DateTime*`. That *could* be C++/CLI but it's likely to be C#, which has a class DateTime. – Banex Aug 02 '15 at 18:33
  • Well, I'm trying to do this in C++ Visual Studio. What classes should I use? actually I don't know the difference between Standard and .NET, but the easier the better.. – Chuck Norris Aug 02 '15 at 18:35
  • "in […] visual studio?" – oh wait, Visual Studio's got a date/time calculator feature? – The Paramagnetic Croissant Aug 02 '15 at 19:04
  • Have you tried searching StackOverflow? Looks like there are a bunch of related, if not duplicate, questions. – Thomas Matthews Aug 02 '15 at 20:03
  • By the way, Visual Studio is a tool for developing C++, C#, C, and .NET/CLR languages to name a few. Visual Studio is not a language. *Be aware that Microsoft adds in features to the standard C and C++ language*, so what you are learning may not be standard C++. – Thomas Matthews Aug 02 '15 at 20:05
  • possible duplicate of [Number of days between two dates C++](http://stackoverflow.com/questions/14218894/number-of-days-between-two-dates-c) – scoa Aug 03 '15 at 06:31

1 Answers1

1

There are a lot of answers in StackOverflow. Try to use search.

For example this one is with good solution: Number of days between two dates C++

#include <iostream>
#include <ctime>

int main()
{
  struct std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */
  struct std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */
  std::time_t x = std::mktime(&a);
  std::time_t y = std::mktime(&b);
  if ( x != (std::time_t)(-1) && y != (std::time_t)(-1) )
  {
      double difference = std::difftime(y, x) / (60 * 60 * 24);
      std::cout << std::ctime(&x);
      std::cout << std::ctime(&y);
      std::cout << "difference = " << difference << " days" << std::endl;
  }
  return 0;
}

If you want to use it with simple way (like in C#) you should use third-part libraries like this: http://howardhinnant.github.io/date_algorithms.html

Community
  • 1
  • 1
Artem Kulikov
  • 2,250
  • 19
  • 32
  • No worries, this was automated message that came from reviewing your answer. So you know for next time, it's better to include essential parts if the link stops working :) – Marko Aug 03 '15 at 06:29