查询某一天是一年中的第几天,ctime的使用
#include <iostream> #include <ctime> using namespace std; int CalcHowManyDays(unsigned int Year, unsigned int Month, unsigned int Day, unsigned int *pSumOfDays) { //结构体tm初始化 struct tm begin = { 0 }; struct tm end = { 0 }; //计算年份-1900 begin.tm_year = Year - 1900; end.tm_year = Year - 1900; //计算月份-1 begin.tm_mon = 1 - 1; end.tm_mon = Month - 1; begin.tm_mday = 1; end.tm_mday = Day; //计算秒的差,再除以一天的秒数86400,计算天数差,+1即为第几天 double second = difftime(mktime(&end), mktime(&begin)); *pSumOfDays = (unsigned int)(second / 86400)+1; return 0; } int main(){ unsigned int SumOfDays=0; CalcHowManyDays(2017,7,10,&SumOfDays); cout<<SumOfDays<<endl; return 0; }