这以前本是一个MFC代码,我在这个基础上修改成了标准C++的..
即可以在VC里用,也可以在C++Builder里用..所以一并放到这里来,
希望有人喜欢..喜欢的就给点鼓励啊~~
示例代码:
头文件:
即可以在VC里用,也可以在C++Builder里用..所以一并放到这里来,
希望有人喜欢..喜欢的就给点鼓励啊~~
示例代码:
代码: |
void __fastcall TForm1::Button1Click(TObject *Sender) { xDate *Date_x=new xDate();//取系统时间初始化-:) Memo1->Lines->Add("公历"+IntToStr(Date_x->GetYear()) +"年"); Memo1->Lines->Add("公历"+IntToStr(Date_x->GetMonth())+"月"); Memo1->Lines->Add("公历"+IntToStr(Date_x->GetDay()) +"日"); if(Date_x->IsLeapYear(Date_x->GetYear())) Memo1->Lines->Add("今年是闰年"); else Memo1->Lines->Add("今年不是闰年"); Memo1->Lines->Add("今天是星期"+IntToStr(Date_x->WeekDay(Date_x->GetYear(),Date_x->GetMonth(),Date_x->GetDay()))); Memo1->Lines->Add("公历:本月"+IntToStr(Date_x->MonthDays(Date_x->GetYear(),Date_x->GetMonth()))+"天"); Memo1->Lines->Add("阴历:本月"+IntToStr(Date_x->LunarMonthDays(Date_x->GetYear(),Date_x->GetMonth()))+"天"); Memo1->Lines->Add("阴历今年:"+IntToStr(Date_x->LunarYearDays(Date_x->GetYear()))+"天"); //==================================================================// char ch0[20]="天干记年法表示:"; char Buffer0[8]; Date_x->FormatLunarYear(Date_x->GetYear(),Buffer0); strcat(ch0,Buffer0); Memo1->Lines->Add(ch0); //==================================================================// char ch1[20]="月份中文表示法:"; char Buffer1[8]; Date_x->FormatMonth(Date_x->GetMonth(),Buffer1,true); strcat(ch1,Buffer1); Memo1->Lines->Add(ch1); //==================================================================// char ch2[20]="Day中文表示法:"; char Buffer2[8]; Date_x->FormatLunarDay(Date_x->GetDay(),Buffer2); strcat(ch2,Buffer2); Memo1->Lines->Add(ch2); //==================================================================// //计算两个日期相差的天数 Memo1->Lines->Add("距1956,2,21有:"+IntToStr(Date_x->CalcDateDiff(Date_x->GetYear(),Date_x->GetMonth(),Date_x->GetDay(),1956,2,21))+"天"); //节气计算 WORD iLunarYear, iLunarMonth, iLunarDay; WORD n; n=Date_x->GetLunarDate(Date_x->GetYear(),Date_x->GetMonth(),Date_x->GetDay(),iLunarYear,iLunarMonth,iLunarDay); if(n) Memo1->Lines->Add(IntToStr(n)); else Memo1->Lines->Add("不是节气"); Memo1->Lines->Add("今天阴历是:"+IntToStr(iLunarYear)+"-"+IntToStr(iLunarMonth)+"-"+IntToStr(iLunarDay)); } //--------------------------------------------------------------------------- |
头文件:
代码: |
//xDate.h /************************************************************************************************************ Author :xShandow Dest :A Date Class For C++ Email :chenzg@hftd.com HomePage:http://www.hftd.com Dev Env :Visual C++6.0 OS. :Windows 2000 Professinal Date :2003-NOV-15 ************************************************************************************************************/ #ifndef XDATE_H #define XDATE_H #include <windows.h> extern const WORD START_YEAR; extern const WORD END_YEAR ; class xDate { private: WORD m_iYear, m_iMonth, m_iDay; void l_InitData(); //计算从1901年1月1日过iSpanDays天后的阴历日期 static void l_CalcLunarDate(WORD &iYear, WORD &iMonth ,WORD &iDay,LONG iSpanDays); //计算公历iYear年iMonth月iDay日对应的节气 0-24,0表不是节气 static WORD l_GetLunarHolDay(WORD iYear, WORD iMonth, WORD iDay); public: //=====================================================================================// xDate(WORD iYear, WORD iMonth, WORD iDay); xDate(); //=====================================================================================// WORD GetYear(){return m_iYear;} WORD GetMonth(){return m_iMonth;} WORD GetDay(){return m_iDay;} //=====================================================================================// void GetDate(WORD &iYear, WORD &iMonth, WORD &iDay); BOOL SetDate(WORD iYear , WORD iMonth , WORD iDay); //=====================================================================================// //判断iYear是不是闰年 static BOOL IsLeapYear(WORD iYear) {return !(iYear%4)&&(iYear%100) || !(iYear%400);} //计算iYear,iMonth,iDay对应是星期几 1年1月1日 --- 65535年12月31日 static WORD WeekDay(WORD iYear, WORD iMonth, WORD iDay); //返回iYear年iMonth月的天数 1年1月 --- 65535年12月 static WORD MonthDays(WORD iYear, WORD iMonth); //返回阴历iLunarYer年阴历iLunarMonth月的天数,如果iLunarMonth为闰月, //高字为第二个iLunarMonth月的天数,否则高字为0 // 1901年1月---2050年12月 static LONG LunarMonthDays(WORD iLunarYear, WORD iLunarMonth); //返回阴历iLunarYear年的总天数 // 1901年1月---2050年12月 static WORD LunarYearDays(WORD iLunarYear); //返回阴历iLunarYear年的闰月月份,如没有返回0 // 1901年1月---2050年1 |