UINT32 i_32u = 1990u, j_u32 = 0u, year_flag = 0u, year_day_u32 = 0u, sum_day_u32 = 0u, weekday_u32 = 0u;
for ( i_32u = 1990u; i_32u <= year_u32; i_32u++)
{
year_flag = Is_Leap_Year(year_u32);
if ( 1u == year_flag )
{
year_day_u32 = 366u;
}
else
{
year_day_u32 = 365u;
}
switch (mouth_u32 - 1u)
{
case 1u:
sum_day_u32 += 31u;
break;
case 2u:
sum_day_u32 += 31u + 28u + year_flag;
break;
case 3u:
sum_day_u32 += 31u + 28u + year_flag + 31u;
break;
case 4u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u;
break;
case 5u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u + 31u;
break;
case 6u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u + 31u + 30u;
break;
case 7u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u;
break;
case 8u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u + 31u;
break;
case 9u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u + 31u + 30u;
break;
case 10u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u + 31u + 30u + 31u;
break;
case 11u:
sum_day_u32 += 31u + 28u + year_flag + 31u + 30u + 31u + 30u + 31u + 31u + 30u + 31u + 30u;
break;
default:
break;
}
}
sum_day_u32 += day_u32;
weekday_u32 = sum_day_u32 % 7u + 3;
if (weekday_u32 != 0u)
{
(void)OSM_Printf("%d年%d月%d日是星期%d\n", year_u32, mouth_u32, day_u32, weekday_u32);
}
else
{
(void)OSM_Printf("%d年%d月%d日是星期天\n", year_u32, mouth_u32, day_u32);
}*/
}
这个代码太水了 ,之后又想了一种办法,而且这样做的话会出现的问题就是不能正确输出争正确的日期的日期,
于是就想到了1990这个好数字,应为1990年1月1日搞好就是星期一
所以整理了一下代码写出了更好的一个函数
UINT32 Week(
UINT32 year_u32,
UINT32 mouth_u32,
UINT32 day_u32
)
{
UINT32 day_code = 0u;
UINT32 i32i = 0u;
UINT32 u32_j = 0u;
UINT32 year_flag = 0u;
UINT32 days = 0u;
UINT32 i32_sum = 0u;
UINT32 MOUTH[12] = {30u, 28u, 31u, 30u, 31u, 30u, 31u, 31u, 30u, 31u, 30u, 31u};
if ( (mouth_u32 < 1u) || (mouth_u32 > 12u) || (day_u32 > 31u) || (year_u32 < 1990u))
{
(void)OSM_Printf("输入有误的数据");
}
else
{
for ( i32i = 1990u ; i32i < year_u32; i32i++)
{
year_flag = Is_Leap_Year(year_u32);
if ( 1u == year_flag)
{
days += 366u;
}
else
{
days += 365u;
}
}
for ( i32i = 0u; i32i < (mouth_u32 - 1u); i32i++)
{
days += MOUTH[i32i];
}
u32_j = days % 7u;
switch(u32_j)
{
case 0u:
(void)OSM_Printf("星期日\n");
break;
case 1u:
(void)OSM_Printf("星期一\n");
break;
case 2u:
(void)OSM_Printf("星期二\n");
break;
case 3u:
(void)OSM_Printf("星期三\n");
break;
case 4u:
(void)OSM_Printf("星期四\n");
break;
case 5u:
(void)OSM_Printf("星期五\n");
break;
case 6u:
(void)OSM_Printf("星期六\n");
break;
default:
break;
}
}
return 0u;
}
原文地址:http://blog.csdn.net/u011046042/article/details/42742037