C++ compare dates to get age
您好我是C ++的新手,特别是C ++中的日期..我怎样才能将这些数字29(天)08(月)86(年)与今天的日期进行比较以获得年龄?
这是我开始我的功能:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | std::string CodeP, day, month, year, age; std::cout<<"Enter Permanent Code(example: SALA86082914) : "; //birthday first six numbers in code std::cin>>CodeP; year = CodeP.substr (4,2); month = CodeP.substr (6,2); day = CodeP.substr (8,2); std::cout<<"day :"<<day <<' '; std::cout<<"month :"<<month <<' '; std::cout<<"year :"<<year <<' '; //then get today's date to compare with the numbers of birthday to get age |
这将计算年龄(年)的近似值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <ctime> #include <iostream> using namespace std; int main(){ struct tm date = {0}; int day, month, year; cout<<"Year:"; cin>>year; cout<<"Month:"; cin>>month; cout<<"Day:"; cin>>day; date.tm_year = year-1900; date.tm_mon = month-1; date.tm_mday = day; time_t normal = mktime(&date); time_t current; time(¤t); long d = (difftime(current, normal) + 86400L/2) / 86400L; cout<<"You have~:"<<d/365.0<<" years. "; return (0); } |
尝试下面的代码,我已经为比较和处理你需要的两个日期做了必要的代码。请注意,我的代码的作用只是比较两个日期,并根据这些日期给出结果年龄。
抱歉,我必须让您完成创建代码的工作,该代码将根据六位数的生日输入为您提供预期的输出。我想你已经知道你仍然需要为你的问题提出自己的解决方案,我们只是在这里为你提供一个如何解决它的想法。我们只能帮助和支持你。希望我的帖子很有帮助!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | class age { private: int day; int month; int year; public: age():day(1), month(1), year(1) {} void get() { cout<<endl; cout<<"enter the day(dd):"; cin>>day; cout<<"enter the month(mm):"; cin>>month; cout<<"enter the year(yyyy):"; cin>>year; cout<<endl; } void print(age a1, age a2) { if(a1.day>a2.day && a1.month>a2.month) { cout<<"your age is DD-MM-YYYY"<<endl; cout<<"\t\t"<<a1.day-a2.day<<"-"<<a1.month-a2.month<<"-"<<a1.year-a2.year; cout<<endl<<endl; } else if(a1.daya2.month) { cout<<"your age is DD-MM-YYYY"<<endl; cout<<"\t\t"<<(a1.day+30)-a2.day<<"-"<<(a1.month-1)-a2.month<<"-"<<a1.year-a2.year;? cout<<endl<<endl; } else if(a1.day>a2.day && a1.month<a2.month) { cout<<"your age is DD-MM-YYYY"<<endl; cout<<"\t\t"<<a1.day-a2.day<<"-"<<(a1.month+12)-a2.month<<"-"<<(a1.year-1)-a2.year; cout<<endl<<endl; } else if(a1.day<a2.day && a1.month<a2.month) { cout<<"your age is DD-MM-YYYY"<<endl; cout<<"\t\t"<<(a1.day+30)-a2.day<<"-"<<(a1.month+11)-a2.month<<"-"<<(a1.year-1)-a2.year; cout<<endl<<endl; } } }; int main() { age a1, a2, a3; cout<<"\t Enter the current date."; cout<<endl<<endl; a1.get(); cout<<"\t enter Date of Birth."; cout<<endl<<endl; a2.get(); a3.print(a1,a2); return 0; } |
首先减去年份以获得差异。现在比较几个月;如果今天的月份大于出生月份,那么你就完成了。如果没有,比较天数;如果今天的日子大于出生日,你就完成了。否则从差异中减去一个,即年龄。
我找到了另一个(更简单的?)答案,使用Boost.Gregorian
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <boost/date_time/gregorian/gregorian.hpp> using namespace boost::gregorian; template<typename Iterator, typename Date> struct date_count : public std::pair<unsigned, Date> { typedef std::pair<unsigned, Date> p; operator unsigned() { return p::first; } operator Date() { return p::second; } date_count(Date begin, Date end) : p(0, begin) { Iterator b { begin }; while( *++b <= end ) ++p::first; p::second = *--b; } }; |
而你只需要这样使用它:
1 2 3 4 5 6 7 8 9 10 11 12 | date bday { from_undelimited_string(std::string("19")+a.substr(4,6)) }; date now = day_clock::local_day(); date_count<year_iterator, date> years { bday, now }; date_count<month_iterator, date> months { years, now }; date_count<day_iterator, date> days { months, now }; std::cout <<"From" << bday <<" to" << now <<" there are" << std::setw(5) << years <<" years," << std::setw(3) << months <<" months," << std::setw(3) << days <<" days "; |
如果您只想要年数,您可以:
1 | date_count<year_iterator> years(bday, now); |
并以"年"变量中的年数运行。 :d
最好的方法是使用Boost.Gregorian:
假设
1 2 3 | #include <boost/date_time/gregorian/gregorian.hpp> using namespace boost::gregorian; |
你会这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | date bday { from_undelimited_string(std::string("19")+a.substr(4,6)) }; date now = day_clock::local_day(); unsigned years = 0; year_iterator y { bday }; while( *++y <= *year_iterator(now) ) ++years; --y; std::cout << *y <<" "; unsigned months = 0; month_iterator m { *y }; while( *++m <= *month_iterator(now) ) ++months; --m; std::cout << *m <<" "; unsigned days = 0; day_iterator d { *m }; while( *++d <= *day_iterator(now) ) ++days; --d; std::cout << *d <<" "; std::cout << years <<" years," << months <<" months," << days <<" days "; |