求输入日期是星期几

/*******************************************************
**
**   功能:求输入日期是星期几   (0表示星期天,其余为   1-6)
**   日期:2006-6-16
**  
**
********************************************************/
int CMy33Dlg::SW_WeekDay(int year, int  month, int day)
{        
              int   DayOfWeek;
              /*下面的四条语句用来计算输入日期的星期数,是程序的核心部分,缺一不可*/
              DayOfWeek=year>0 ? (5 + (year+ 1)+(year-1)/4 -(year-1)/100 + (year-1)/400)%7  
                            : (5 + year + year/4 - year/100 + year/400) % 7;              
              DayOfWeek = month > 2 ? (DayOfWeek + 2*(month + 1) + 3*(month + 1)/5)   %   7  
                            :   (DayOfWeek   +   2*(month   +   2)   +   3*(month   +   2)/5)   %   7;
              if (((year%4 == 0 && year%100 != 0) || year%400 == 0) && month>2)
     {
                 DayOfWeek = (DayOfWeek + 1) % 7;
              }
              DayOfWeek = (DayOfWeek + day) % 7;
     CString strtemp;
    switch(DayOfWeek)
    {
    case 0:
     strtemp="今天是星期日";
     break;
    case 1:
     strtemp="今天是星期一";
     break;
    case 2:
     strtemp="今天是星期二";
     break;
    case 3:
     strtemp="今天是星期三";
     break;
    case 4:
     strtemp="今天是星期四";
     break;
    case 5:
     strtemp="今天是星期五";
     break;
    case 6:
     strtemp="今天是星期六";
     break;
    default:
     strtemp="请你查看年月日是否输入完全!";
     break;     
    }
    
             MessageBox(strtemp);
              return   DayOfWeek;
}
//功能:调用SW_WeekDay()函数,查看是星期几
void CMy33Dlg::OnbtnWeek()
{
  UpdateData();
  int year,month,day;
  if(m_editStrYear!=""&&m_editStrMonth!=""&&m_editStrDay!="")
  {
     year=atoi(m_editStrYear);    //m_eidtStrYear 为编辑框的变量名
     month=atoi(m_editStrMonth);
     day=atoi(m_editStrDay);
  }
    SW_WeekDay(year,month,day); //调用计算星期几的函数 
  UpdateData(false);
}

posted on 2006-06-16 13:30  路吾  阅读(526)  评论(0编辑  收藏  举报

导航