Calculate total number of days since first year of era
Yup, this is weird question but this calculate can be used in calculating difference between two dates.
So here is the calculation (fast and efficient):
Thanks
So here is the calculation (fast and efficient):
#include <iostream> #include <conio.h> using namespace std; int month[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; struct Date { int y, d, m; Date(int day, int month, int year) { d = day; m = month; y = year; } }; int countLeapYears(Date d) { int years = d.y; if (d.m <= 2) years--; return years / 4 - years / 100 + years / 400; } long CalculateDay(Date date) { return (date.y - 1)* 365 + date.d + month[date.m - 1]; } void main() { Date date(14, 12, 1986); long result = CalculateDay(date) + countLeapYears(date); cout << result << " days" << endl; _getch(); }
Thanks
Comments
Post a Comment