User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,961 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4365 | Replies: 3
Reply
Join Date: Nov 2006
Posts: 6
Reputation: hui is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
hui hui is offline Offline
Newbie Poster

Help Calculating Time Difference

  #1  
Feb 23rd, 2007
Hi guys..I am trying to make a program that can calculate the time difference in days from the current date..here is my code...it is bringing some bit of a problem...please help me out!Thanks

#include <iostream.h>
#include <cdate.h>
 
class Date
 
{
 
public:
 
 int month;
 
 int day;
 
 int year;
 
 static int dtab[2][13];
 
public:
 int getMonth()
 
 {
  return month;
 }
 
 int getDay()
 
 {
  return day;
 }
 
 int getYear()
 
 {
  return year;
 }
 
 Date operator-(Date& d2);
 
 Date& operator-()
 {
  month=-month;
 
  day=-day;
 
  year=-year;
 
  return *this;
 }
 int compare(Date&);
 
 int operator<(Date& d2)
 
 {
 
  return compare{d2) < 0;
 
 }
 
 int operator<=(const Date& d2) 
 
    {
 
  return compare(d2) <= 0;
 
 }
 
   int operator>( Date& d2) 
 
    {
 
    return compare(d2) > 0;
 
 }
 
   int operator>=( Date& d2) 
 
     {
 
    return compare(d2) >= 0;
 
 }
 
   int operator==( Date& d2) 
 
     {
 
    return compare(d2) == 0;
 
 }
 
   int operator!=( Date& d2) 
 
     {
 
    return compare(d2) != 0;
 
 }
 
 static int isleap(int y)
 
     {
 
  return y%4 == 0 && y%100 != 0 || y%400 == 0;
 
 }
 
 };
 
int main()
 
{  
 
 Date today, d2;
 
 cout << "Today's date is "<< today << endl;
 
 cout << "Enter another date: ";
 
 cin >> d2;
 
 cout << "today -d2 = "<< today - d2 << endl;
 
 cout << "d2 - today = "<< d2 - today << endl;
 
   return 0;
 
}
 
 
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Calculating Time Difference

  #2  
Feb 23rd, 2007
#1: You don't need to double space everything. It's too hard to read.
#2: You need to indent consistently, and 3-4 spaces, not 1 or 2
#3: "it is bringing some bit of a problem" has no meaning. What is the problem. Describe it, don't make us guess.
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Jan 2007
Posts: 166
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Calculating Time Difference

  #3  
Feb 23rd, 2007
Originally Posted by hui View Post
Hi guys..I am trying to make a program that can calculate the time difference in days from the current date..here is my code...it is bringing some bit of a problem...please help me out!Thanks

#include <iostream.h>
#include <cdate.h>
 
class Date
 
{
 
public:
 
 int month;
 
 int day;
 
 int year;
 
 static int dtab[2][13];
 
public:
 int getMonth()
 
 {
  return month;
 }
 
 int getDay()
 
 {
  return day;
 }
 
 int getYear()
 
 {
  return year;
 }
 
 Date operator-(Date& d2);
 
 Date& operator-()
 {
  month=-month;
 
  day=-day;
 
  year=-year;
 
  return *this;
 }
 int compare(Date&);
 
 int operator<(Date& d2)
 
 {
 
  return compare{d2) < 0;
 
 }
 
 int operator<=(const Date& d2) 
 
    {
 
  return compare(d2) <= 0;
 
 }
 
   int operator>( Date& d2) 
 
    {
 
    return compare(d2) > 0;
 
 }
 
   int operator>=( Date& d2) 
 
     {
 
    return compare(d2) >= 0;
 
 }
 
   int operator==( Date& d2) 
 
     {
 
    return compare(d2) == 0;
 
 }
 
   int operator!=( Date& d2) 
 
     {
 
    return compare(d2) != 0;
 
 }
 
 static int isleap(int y)
 
     {
 
  return y%4 == 0 && y%100 != 0 || y%400 == 0;
 
 }
 
 };
 
int main()
 
{  
 
 Date today, d2;
 
 cout << "Today's date is "<< today << endl;
 
 cout << "Enter another date: ";
 
 cin >> d2;
 
 cout << "today -d2 = "<< today - d2 << endl;
 
 cout << "d2 - today = "<< d2 - today << endl;
 
   return 0;
 
}
 
 


http://www.cprogramming.com/
http://www.cplusplus.com/doc/tutorial/
http://www.codersource.net/codersour...ogramming.html

Good luck, LamaBot
Last edited by Lazaro Claiborn : Feb 23rd, 2007 at 3:39 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,721
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Calculating Time Difference

  #4  
Feb 23rd, 2007
difference in days between two dates is very easy to calculate.
1. get time_t for first date. If this is not today's date, then fill out a struct tm with day, month-1, year-1900 -- make all other structure members 0, then pass your struct tm object to mktime(), which will convert it to time_t.
2. get time_t for second date
3. subtract them -- this is difference between two dates in seconds
4. there are 24 * 60 * 60 seconds in one day. So take the result of step 3 above and divide by the number of seconds in one day.
5. finished.

  1. #include <iostream.h>
  2. #include <cdate.h>
1. iostream.h is very old and obsolete. current version does not have .h file extension. If you are using an acient compiler such as Turbo C++ then this is ok because your compiler probably does not support the new file naming convention.

2. cdate.h -- there is no such file. use either date.h or cdate (no file extension). This is how it might appear for modern compilers.

  1. #include <iostream>
  2. #include <cdate>
Last edited by Ancient Dragon : Feb 23rd, 2007 at 5:37 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:05 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC