最看近在网上看到一个某年某月某是星期几的例子,因此写一个完整的函数
由于简单,只贴相关代码
星期天是0 星期六是6
//
0~6
int
DayOfWeek(
int
iYear,
int
iMonth,
int
iDay)
{
int iWeek =- 1 ;
if (iYear < 1111 || iYear > 9999 )
return iWeek;
if (iMonth <= 0 || iMonth > 12 )
return iWeek;
if (iDay <= 0 || iDay > 31 )
return iWeek;
int iAllDays[] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 };
bool leap_year = ((iYear % 4 == 0 ) && (iYear % 100 != 0 )) || (iYear % 400 == 0 );
if (leap_year)
{
iAllDays[ 1 ] = 29 ;
}
else
{
iAllDays[ 1 ] = 28 ;
}
LPSYSTEMTIME lpSystemTime = NULL;
lpSystemTime = (LPSYSTEMTIME) new BYTE( sizeof (LPSYSTEMTIME));
::GetSystemTime(lpSystemTime);
int iDays = 0 ;
if (lpSystemTime -> wMonth == iMonth)
{
iWeek = lpSystemTime -> wDayOfWeek;
}
else if (lpSystemTime -> wMonth > iMonth)
{
for ( int i = (lpSystemTime -> wMonth - 1 );i >= iMonth;i -- )
{
iDays += iAllDays[i - 1 ];//获得当前月的前一个月到所求月的天数
}
iDays += lpSystemTime -> wDay; //加上当前朋的天娄
iDays -= (lpSystemTime -> wDayOfWeek + 1 ); //减去当前月(星期天数)
iDays -= iDay - 1 ;
iWeek = iDays % 7 ;
if (iWeek != 0 )
{
iWeek = 7 - iWeek;
}
}
else
{
for ( int i = (lpSystemTime -> wMonth + 1 );i < iMonth;i ++ )
{
iDays += iAllDays[i - 1 ];
}
iDays += iAllDays[lpSystemTime -> wMonth - 1 ] - lpSystemTime -> wDay;
iDays -= ( 7 - (lpSystemTime -> wDayOfWeek));
iDays += iDay;
iWeek = iDays % 7 ;
}
return iWeek;
}
{
int iWeek =- 1 ;
if (iYear < 1111 || iYear > 9999 )
return iWeek;
if (iMonth <= 0 || iMonth > 12 )
return iWeek;
if (iDay <= 0 || iDay > 31 )
return iWeek;
int iAllDays[] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 };
bool leap_year = ((iYear % 4 == 0 ) && (iYear % 100 != 0 )) || (iYear % 400 == 0 );
if (leap_year)
{
iAllDays[ 1 ] = 29 ;
}
else
{
iAllDays[ 1 ] = 28 ;
}
LPSYSTEMTIME lpSystemTime = NULL;
lpSystemTime = (LPSYSTEMTIME) new BYTE( sizeof (LPSYSTEMTIME));
::GetSystemTime(lpSystemTime);
int iDays = 0 ;
if (lpSystemTime -> wMonth == iMonth)
{
iWeek = lpSystemTime -> wDayOfWeek;
}
else if (lpSystemTime -> wMonth > iMonth)
{
for ( int i = (lpSystemTime -> wMonth - 1 );i >= iMonth;i -- )
{
iDays += iAllDays[i - 1 ];//获得当前月的前一个月到所求月的天数
}
iDays += lpSystemTime -> wDay; //加上当前朋的天娄
iDays -= (lpSystemTime -> wDayOfWeek + 1 ); //减去当前月(星期天数)
iDays -= iDay - 1 ;
iWeek = iDays % 7 ;
if (iWeek != 0 )
{
iWeek = 7 - iWeek;
}
}
else
{
for ( int i = (lpSystemTime -> wMonth + 1 );i < iMonth;i ++ )
{
iDays += iAllDays[i - 1 ];
}
iDays += iAllDays[lpSystemTime -> wMonth - 1 ] - lpSystemTime -> wDay;
iDays -= ( 7 - (lpSystemTime -> wDayOfWeek));
iDays += iDay;
iWeek = iDays % 7 ;
}
return iWeek;
}