•
•
•
•
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
![]() |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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;
}
•
•
•
•
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,721
Reputation:
Rep Power: 36
Solved Threads: 884
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. 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. 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.
c Syntax (Toggle Plain Text)
#include <iostream.h> #include <cdate.h>
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.
c Syntax (Toggle Plain Text)
#include <iostream> #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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Time complexity of algorithm (Computer Science and Software Design)
- Time Difference with precision (C)
- Time difference (MS Access and FileMaker Pro)
- time difference (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Problems installing MySQL++ wrapper for C++ language
- Next Thread: Study problem



Linear Mode