Simple date functions in Microsoft VC++ 6

Hi,

I need to do the following ('simple things') with dates in VC++:
- Obtain todays date in dd/mm/yy format
- Pass a stored date (stored as 3 integers - d,m,y) into a 'date' variable
- Compare two dates, and obtain the number of days between them (this should allows for negative values)
- Add a number of days to a date, and return a valid date which can be stored again in three integers

I have managed to get as far as obtaining todays date, and saving as three integers:
      char dateToEncode[9], tempDateBits[3];
      int todayDay, todayMonth, todayYear;

      _strdate(dateToEncode);

      //Splitting the date into components
      tempDateBits[0] = dateToEncode[0];
      tempDateBits[1] = dateToEncode[1];
      todayMonth = atoi(tempDateBits);

      tempDateBits[0] = dateToEncode[3];
      tempDateBits[1] = dateToEncode[4];
      todayDay = atoi(tempDateBits);

      tempDateBits[0] = dateToEncode[6];
      tempDateBits[1] = dateToEncode[7];
      todayYear = atoi(tempDateBits);

But cannot do any of the calculations...

Would really appreciate any suggestions!

Thanks in advance,
Bob.
capsoftukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dimitryCommented:
You need to add '\0' string termination to allow your atoi() to work...
Something like this:
     tempDateBits[0] = dateToEncode[0];
     tempDateBits[1] = dateToEncode[1];
     tempDateBits[2] = '\0';
     todayMonth = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[3];
     tempDateBits[1] = dateToEncode[4];
     tempDateBits[2] = '\0';
     todayDay = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[6];
     tempDateBits[1] = dateToEncode[7];
     tempDateBits[2] = '\0';
     todayYear = atoi(tempDateBits);
0
capsoftukAuthor Commented:
Thanks for the help. This bit works fine regardless, but I will alter my code anyway, just to keep it neat!
0
dimitryCommented:
Actually, you need to set '\0' only once, so next code is also Ok:
     tempDateBits[0] = dateToEncode[0];
     tempDateBits[1] = dateToEncode[1];
     tempDateBits[2] = '\0';
     todayMonth = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[3];
     tempDateBits[1] = dateToEncode[4];
     todayDay = atoi(tempDateBits);

     tempDateBits[0] = dateToEncode[6];
     tempDateBits[1] = dateToEncode[7];
     todayYear = atoi(tempDateBits);
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

allmerCommented:
What system are you working on?
in windows CTime may solve all your problems.
CTime time;
//This may be the class you want to use.
//Get the current time:
currTime = CTime::GetCurrentTime();

Otherwise have a look at this page:
http://www.codeproject.com/datetime/datetimedisc.asp

Hope it helps,
Jens
0
AxterCommented:
You can use strftime to format a string in your desired format
0
AxterCommented:
See following example:

char time_str[99] = {0};
time_t current_time = time(NULL);
struct tm *time_data = localtime(&current_time);
strftime(time_str, 30, "%d/%m/%y", time_data);
0
AxterCommented:
Correction:
strftime(time_str, sizeof(time_str), "%d/%m/%y", time_data);
0
AxterCommented:
Here's an MFC example:
CTime currentTime(CTime::GetCurrentTime());
CString TimeFormated = currentTime.Format("%d/%m/%y");
0
Jaime OlivaresSoftware ArchitectCommented:
> Compare two dates, and obtain the number of days between them (this should allows for negative values)
> Add a number of days to a date, and return a valid date which can be stored again in three integers

About this, you can use MFC CTimeSpan class

CTime a, b;
CTimeSpan s;

You can make additions and substractions, like these:
s = a - b;
b += s;

Also you can use it as:
int d = s.GetDays();

Have a look to:
http://msdn.microsoft.com/library/en-us/vclib/html/_MFC_CTimeSpan.asp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
capsoftukAuthor Commented:
Hi guys,

Thanks for all the feedback. Unfortuanately I forgot to specify that I was trying to avoid MFC (I hear it has more dependencies, which I am trying to avoid - I am just creating a 'simple' dll).

Anyway, in the end I just wrote my own class:

//Only works with dates between 1/1/2000 and 31/12/2099
class rjedate
{
public:
      rjedate();
      rjedate(int dd, int mm, int yyyy);
      rjedate::rjedate(const rjedate& copyDate);
      ~rjedate();
      void setSide(double s);
      long rjedate::dateToDays(void);
private:
      int day, month, year;
};

rjedate::rjedate()
{
      day = 1;
      month = 1;
      year = 2000;
}

rjedate::rjedate(const rjedate& copyDate)
{
      day = copyDate.day;
      month = copyDate.month;
      year = copyDate.year;
}

rjedate::rjedate(int dd, int mm, int yyyy)
{
      if (yyyy < 2000) yyyy = 2000 + (yyyy % 100);
      if (dd < 1 || dd > 31) dd = 1;
      if (mm < 1) mm = 1;
      if (mm > 12) mm = 12;
      day = dd;
      month = mm;
      year = yyyy;
}

rjedate::~rjedate()
{
      day = -1;
      month = -1;
      year = -1;
}

//Number of Days from 01/01/2000
long rjedate::dateToDays(void)
{
      long tempYears, tempYearsA, tempYearsB, totalDays;

      //Work out whole years
      tempYears = this->year % 100;
      tempYearsA = (long) (tempYears + 3) / 4;
      tempYearsB = tempYears - tempYearsA;
      totalDays = tempYearsA * 366 + tempYearsB * 365;

      //Work out months
      switch(this->month)
      {
      case 1: totalDays += 0; break;
      case 2: totalDays += 31; break;
      case 3: totalDays += 59; break;
      case 4: totalDays += 90; break;
      case 5: totalDays += 120; break;
      case 6: totalDays += 151; break;
      case 7: totalDays += 181; break;
      case 8: totalDays += 212; break;
      case 9: totalDays += 243; break;
      case 10: totalDays += 293; break;
      case 11: totalDays += 304; break;
      case 12: totalDays += 334; break;
      }
      //if there is a Feb 29th, add this day
      if(this->year % 4 == 0 && this->month > 2) totalDays++;

      //Finally, add the days...
      totalDays += this->day;

      return(totalDays);
}

I have distributed the points between you, as it was my fault for not specifying.

Thanks again for your help, and if you can see any flaws with my code, please let me know!

Bob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.