| | |
Calculating Time Difference
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
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
C++ Syntax (Toggle Plain Text)
#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; }
#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.
#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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
#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.cplusplus.com/doc/tutorial/
http://www.codersource.net/codersour...ogramming.html
Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 23rd, 2007 at 4:39 pm.
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 6:37 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Time complexity of algorithm (Computer Science)
- 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
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






