VC 求指定日期是该年度的第几周算法



intGetWeekofYear(SYSTEMTIME stCur)
{
SYSTEMTIME stfirst;


SYSTEMTIME stsecond;
memset(&stsecond,0, sizeof(SYSTEMTIME));
stsecond.wDay=stCur.wDay;
stsecond.wMonth=stCur.wMonth;
stsecond.wYear=stCur.wYear;


CTime tsecond(stsecond);
    CTimeSpan ts;
int nday;
int fristweekday=CaculateWeekDay(stCur.wYear,1,1);
memset(&stfirst,0, sizeof(SYSTEMTIME));
stfirst.wDay=7-fristweekday;
stfirst.wMonth=1;
stfirst.wYear=stCur.wYear;
CTime tfirst(stfirst);
ts=tsecond-tfirst;
    nday=ts.GetDays();
if(nday<0)
return 1;
return nday/7+1+(nday%7==0?0:1);

//认为周日是每周的第一天同计算机显示
//               从1月1号开始算第一周如:1.1是星期六 1.2是星期日 那么1.1就是第一周虽然只有一天但也是第一周,1.2日就是第2周了
//

}

//算任意一天是星期几

int CaculateWeekDay(int nYear,int nMonth, int nDay)
{//基姆拉尔森计算公式
if(nMonth == 1)
{
nMonth = 13;
nYear=nYear-1;
}
if( nMonth == 2)
{
nMonth = 14;
nYear=nYear-1;


}
int nWeek = (nDay+2*nMonth+3*(nMonth+1)/5+nYear+nYear/4-nYear/100+nYear/400)%7;
int strWeekDay = 0;
switch(nWeek)
{
case 0: strWeekDay =1; break;//星期一
case 1: strWeekDay =2; break;
case 2: strWeekDay =3; break;
case 3: strWeekDay =4; break;
case 4: strWeekDay =5; break;
case 5: strWeekDay =6; break;
case 6: strWeekDay =0; break;//星期日

return strWeekDay;
}

你可能感兴趣的:(算法)