I want to subtract date A from date B. Both are stored as mm/dd/yyyy.
Googling on datediff - as known in vb and subtract dates all with c++ hasn't yields much.
Any std or time.h functions or am I left to write my own?
Printable View
I want to subtract date A from date B. Both are stored as mm/dd/yyyy.
Googling on datediff - as known in vb and subtract dates all with c++ hasn't yields much.
Any std or time.h functions or am I left to write my own?
The Boost Date-time library has functionality for this. You can literally subtract two date objects, using the minus operator.
http://www.boost.org/doc/html/date_time.html
You could put your yyyy, mm, and dd into a struct tm, use mktime to convert to a time_t, and then subtract two time_ts with difftime (if both dates are within the time's epoch). Something similar to this, perhaps.
You can also use the code available in the FAQ for actually timing how long your program takes to run.Code:#include <iostream>
#include <time.h>
using namespace std;
int main()
{
time_t current_time,past_time;
time(¤t_time);
cout<<"Current time in time_t format is "<<current_time<<endl;
cout<<"Current time in ctime format is "<<ctime(¤t_time)<<endl;
past_time = current_time -1000;
cout<<"\nPast time in time_t format is "<<past_time<<endl;
cout<<"Past time in ctime format is "<<ctime(&past_time)<<endl;
cin.get();
return 0;
}
Thanks to all.
Dave - that code is perfect.
There is always the option of using Julian dates, as well:Code:#include <iostream>
#include <iomanip>
using namespace std;
#define GREGORIAN
const char *DAY_OF_WEEK[] = { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday" };
void print_julian(double julian_date) {
int Z = julian_date + 0.5;
int W = (Z - 1867216.25) / 36524.25;
int X = W / 4;
int A = Z + 1 + W - X;
int B = A + 1524;
int C = (B - 122.1) / 365.25;
int D = 365.25 * C;
int E = (B - D) / 30.6001;
int F = 30.6001 * E;
int day_of_month = B - D - F;
int month = E > 12 ? (E - 13) : (E - 1);//E - 1 or E-13 (must get number less than or equal to 12)
int year = month <= 2 ? (C - 4715) : (C - 4716);//(if Month is January or February) or C-4716 (otherwise)
cout << month << "/" << day_of_month << "/" << year << endl;
}
int main() {
int month = 8,
year = 2005,
day = 23;
double hour = 9,
minute = 16,
second = 30;
int a = (14 - month) / 12;
int y = year + 4800 - a;
int m = month + (12 * a) - 3;
#ifdef GREGORIAN
// for a date in the Gregorian calendar (at noon)
int jdn = day +
(((153 * m) + 2) / 5) +
(365 * y) +
(y / 4) -
(y / 100) +
(y / 400) -
32045;
#else
// for a date in the Julian calendar (at noon)
int jdn = day + (((153 * m) + 2) / 5) + (365 * y) +
(y / 4) - 32083;
#endif
double julian_date = jdn + ((hour - 12) / 24) + (minute / 1440) + (second / 86400);
cout << "The day of the week is " <<
DAY_OF_WEEK[static_cast<int>(julian_date) % 7] << "." << endl;
cout << "Today is ";
print_julian(julian_date);
julian_date -= 40;
cout << "Forty days ago it was ";
print_julian(julian_date);
return 0;
}
Quote:
Originally Posted by the output of that program
this code return this error :
12 E:\WKC\Untitled1.cpp [Warning] converting to `int' from `double' .
(if it is my mistake please teach me, im new to c++)
It is a warning (not an error), and I think its message is pretty clear. difftime returns a double, which is being implicitly converted to an int -- thus drawing the warning.Quote:
Originally Posted by wyvern