Date arithmetic

Pls can someone help me with guideline on how to write a program to calculate date arithmetic (e.g. no. of days b/w Jan 6, 1990 and August 3, 1992. Am just a beginner.
In C++, there is a file you can include called ctime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>

int main()
{
  time_t seconds1, seconds2, dif;
  seconds1 = /*Number here accordingly*/
  seconds2 = /*Number here accordingly*/
  dif = difftime (seconds1, seconds2);

  cout << dif << endl;

  return 0;
}


Seconds gives a reference to number of seconds since Jan 1st, 1970, when used like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
/* time example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t seconds;

  seconds = time (NULL);
  printf ("%ld hours since January 1, 1970", seconds/3600);
  
  return 0;
}


The ctime library is where you want to start looking. I don't want to give you the exact answer, but here is a link to more info on the <ctime> library.

http://www.cplusplus.com/reference/clibrary/ctime/time/

Good luck!
Last edited on
Thanks man. I appreciate it a lot. I'll just try it out now
Topic archived. No new replies allowed.