×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do I subtract 2 date fields??

How do I subtract 2 date fields??

How do I subtract 2 date fields??

(OP)
Hi,
In C++ I want to add time (in hours and minutes) to a date field containing the time in hours and minutes along with the date. Also I want to find the difference in time (in minutes) between 2 given dates(with time). How can I do that? Is there any class available or library available in C++ that would ease the job of creating a new one of my own and overriding the operators?
Thanks
Rgds,
Sudha

Visit www.ssudhakar.com to know more about me

RE: How do I subtract 2 date fields??

You could create your own class with functions GetTime, ConvertTime, DisplayTime and 4 private data members:

  • Days
  • Hours
  • Minutes
  • Total_Minutes

Two constructors, one with no parameters and another to initialize all data members.
You could make a friend function that would overload - operator.
In your main function you have two instances of your class, to sunstract one field from another:
 ...
diff = Instance1.Total_Minutes - Instance.Total_Minutes;
 ...

friend int operator - (Your_Class var1, Your_Class var2)
{
     return (Total_Minutes - var2.Total_Minutes);
}

Best Regards,

aphrodita@mail.krovatka.ru     {uiuc rules}
http://www.hellforge.org

RE: How do I subtract 2 date fields??

(OP)
The problem is that if I add 15 hours to 23:30, it will be a different calculation as the date also changes. how do i keep track of that?
rgds,
Sudha

Visit www.ssudhakar.com to know more about me

RE: How do I subtract 2 date fields??

1) When you add hours, you have to take modulo 24 and reassign the remainder to the TotalHours
if you add only minutes, take modulo 60 and so on.
2) Declare another variable, change. When you add something you will need to do this :

 change = AddedValue div 24;

You check if it equals to one then you add another hour. The same procedure for days and minutes

Best Regards,

aphrodita@mail.krovatka.ru     {uiuc rules}
http://www.hellforge.org

RE: How do I subtract 2 date fields??

(OP)
I have created a Time class as suggested by u..Thank you so so much for the post.Cant tell u how much it helped me
This is my file
#ifndef TIME_H
#define TIME_H
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
class Time
{
private:
    int hours,minutes;
   char *time;
  public:
  Time()
  {
  int hours=minutes=0;
  }
  Time(char *tim)
   {
    char *min=strpbrk(tim,":");
    *min++='\0';
    minutes=atoi(min);
    hours=atoi(tim);
    cout<<" the hours passed is"<<hours<<" and minutes passed is "<<minutes<<endl;
    }
 int operator- (Time t1)
    {
if(hours<t1.hours||((hours==t1.hours)&&(minutes<t1.minutes)))
 {
  cerr<<"the calling time time is lesser than called time";
  return 0;
 }
     if (minutes<t1.minutes)
     {
        minutes+=60;

     }

//        cout<<"value of called minutes is"<<t1.minutes<<endl;
       int mindiff=minutes-t1.minutes;
//cout<<"calling hour="<<hours<<endl;
//cout<<"called hour="<<t1.hours<<endl;
  int  hourdiff;
        if (minutes<t1.minutes)
        hourdiff=hours-t1.hours-1;
       else
      hourdiff=hours-t1.hours;
//cout<<"difference in hours="<<hourdiff<<endl;
       int diffinmin=((hourdiff*60)+mindiff);
    return diffinmin;
    }
   Time operator+(Time t2)
    {
    Time sum;
      sum.minutes=minutes+t2.minutes;
        if(sum.minutes>60)
        {
         sum.minutes-=60;
         sum.hours=sum.hours+1;
         }
         sum.hours=sum.hours+hours+t2.hours;
         if (sum.hours>24)
         {
         sum.hours=sum.hours-24;
         }
         return sum;
      }
   friend ostream& operator<< (ostream&,Time& t3)
      {
       cout<<"The time is "<<setfill('0')<<setw(2)<<t3.hours<<":"<<setfill('0')<<setw(2)<<t3.minutes<<endl;
      }

 };
 #endif

I am having a city that has time as one of its private data. So can I have a constructor that takes a Time datatype? If yes, later, how do I access the private data members of Time? Can I have something like the following?
create object of
Time time inside City class
City *c1;
Time t=c1->time ?
ANd if this is the way, how do I retrieve the data in time? Can I use a double arrow?c1->time->hours;
Sorry if I am giving u a lot of data to think over.
Even if u r not able to find time to answer my maybe stupid question, I am very grateful to u for ur help to get me started in thinking in OO terms.
Rgds,
Sudha

Visit www.ssudhakar.com to know more about me

RE: How do I subtract 2 date fields??

You could make your Time class a friend of City. So that a public function of City could access the private data of the Time.
You could have a int variable, for example, int CityTime;
Then, CityTime = c1->GetTime();
Where c1 is a pointer to your City object, and GetTime() is its function that can retreive value of TotalTime from Time object if it were friend.
To make one class friend of the other in the public section of its declaration you should have:
 ...
public:
   friend class City
 ...
this will make Time friend of City.

Best Regards,

aphrodita@mail.krovatka.ru     {uiuc rules}
http://www.hellforge.org

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close