参考网址:http://blog.sina.com.cn/s/blog_5f571dba0100cvjl.html

修改了下,把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。源代码对这个问题没有减年份。
但是我测试了下,结果是一样的。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include <vector>
#include <iostream>
#include <string>
using namespace std;

string CaculateWeekDay(int y, int m, int d)  
{  
    if(m==1)  
    {
        m=13;
        y--;
    }
    if(m==2)   
    {
        m=14;
        y--;
    }
    int   week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;    
    string   weekstr="";  
    switch(week)  
    {  
        case   0:   weekstr="星期一";   break;  
        case   1:   weekstr="星期二";   break;  
        case   2:   weekstr="星期三";   break;  
        case   3:   weekstr="星期四";   break;  
        case   4:   weekstr="星期五";   break;  
        case   5:   weekstr="星期六";   break;  
        case   6:   weekstr="星期日";   break;  
    }     
    return   weekstr;    
}  

int main(int argc, char* argv[])
{
    cout<<CaculateWeekDay(2012, 6, 12)<<endl;
    return 0;
}

相关文章:

  • 2022-02-25
  • 2021-12-02
  • 2022-12-23
  • 2022-01-07
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2022-02-11
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
  • 2021-07-18
  • 2021-07-28
  • 2022-12-23
相关资源
相似解决方案