`

对任何一天是星期几算法的实现

阅读更多

方法1:

private string str_week(int y,int m,int d){  
   int a=7; // 用来保存计算得到的星期几的整数
   string str_date="";
   if((m==1)||(m==2))//如果是一月或二月进行换算
   {
    m+=12;
    y--;
   }
   a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;   //得到的星期几的整数
   switch (a){ 
    case 0:
     str_date="星期一";
     break;
    case 1:
     str_date="星期二";
     break;
    case 2:
     str_date="星期三";
     break;
    case 3:
     str_date="星期四";
     break;
    case 4:
     str_date="星期五";
     break;
    case 5:
     str_date="星期六";
     break;
    case 6:
     str_date="星期日";
     break;
   }
   return str_date; 
}

方法2:

private string str_week(int y,int m,int d){

string str_date = y.ToString() + "-" + m.ToString() + "-" + d.ToString();

return DateTime.Parse(str_date ).ToString("ddd",new System.Globalization.CultureInfo("zh-cn"))  //返回 zh-cn区域的星期

}

分享到:
评论
2 楼 xuzhenAAfufang 2008-07-04  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
1 楼 xuzhenAAfufang 2008-07-04  
a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;a=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7              m+=12;
   y--;

相关推荐

    查询某一天是星期几.rar

    在编程和计算机科学中,查询某一天是星期几是一个常见的日期处理问题,涉及到日历算法。这个任务可以通过多种编程语言来实现,例如Python、Java、C++等。下面我们将详细探讨这个问题,以及如何使用不同的方法来解决...

    日历查询的算法 如何计算某一天是星期几

    日历查询的算法是计算机科学中一个常见的问题,特别是在日期处理和时间管理的场景中。...通过理解和应用这个公式,我们可以轻松地解决“某一天是星期几”的问题,而无需依赖日历或者复杂的历法知识。

    万年历星期的算法(C语言).rar

    通过学习和实践万年历星期的算法,不仅可以提升对日期处理的理解,还能增强对C语言逻辑控制和数值计算的运用能力。同时,这也可以作为进一步探索日期和时间处理、算法优化等领域的一个起点。在实际工作中,这种算法...

    输入日期,查看当天是星期几

    最常用的方法是蔡勒(Zeller's)公式,这是一个用于计算任何公历日期的星期几的算法。蔡勒公式考虑了年份、月份和日期,通过一系列数学运算得到一个模7的余数,这个余数对应的就是星期几,其中0表示星期六,1表示...

    计算某日为星期几

    每个`case`后面的数字对应于一周中的某一天,而相应的代码块则会处理这一天的逻辑,例如输出这一天的中文名称。 下面是一个可能的实现示例: ```cpp #include using namespace std; // 自定义函数,假设返回值为...

    判断具体一天是星期几

    本示例中的程序设计目标是根据用户输入的日期来确定这一天是星期几。这个功能在许多应用中都有用到,例如日历应用程序或者需要对特定日期进行分析的项目。 首先,我们需要了解基础的日期计算逻辑。在公历系统中,...

    C_实现的根据日期得到今天是星期几

    根据提供的文件信息,本文将详细解释如何在C#中实现基于日期计算出今天是星期几的功能。这包括了具体的算法公式、代码实现方法以及利用C#内置类进行日期操作的相关知识点。 ### 一、算法原理 为了计算一个给定日期...

    1.提供菜单方式选择月日, 2.输入年份、月份、日期,计算得到的是这一天据今天有多少天,星期几;

    在C语言编程中,设计一个程序来实现标题和描述中提到的功能,即通过菜单选择输入年、月、日,然后计算出所选日期距离当前日期的天数以及那天是星期几,是一项常见的日期处理任务。这涉及到对日期的数学运算、循环...

    计算2000~2099年任一天星期几

    标题中的“计算2000~2099年任一天星期几”是一个经典的时间算法问题,涉及到日期处理和模运算。在这个问题中,我们被要求编写一个程序,能够接收一个日期(年、月、日),然后确定这个日期在星期中的位置,即星期一...

    万年历星期的算法(C语言)

    ### 三、2000年至2099年的星期算法实现 #### 函数定义 `WeekDay20` 此函数接收三个参数:年份(`y`)、月份(`m`)和日期(`d`),并返回这一天的星期数和当月的天数。 ##### 实现细节 1. **提取月表**:通过`WeekTab...

    ACM算法模板库各种常用的算法模板

    ACM 算法模板库中提供了相关的算法模板,用于实现求某一天星期几。 ACM 算法模板库提供了各种常用的算法模板,为算法爱好者和竞赛选手提供了便捷的编程体验。这些算法模板可以帮助选手快速解决算法竞赛中的问题,...

    C++实现输入任意日期,并算出该日期是星期几

    在编程领域,尤其是在C++中,计算任意日期是星期几是一项常见的任务,这涉及到日期处理和日历算法。本文将详细讲解如何使用C++来实现这个功能,主要围绕以下几个方面展开: 1. **日期表示**:在C++标准库中,`...

    根据日期计算星期几的C语言代码

    - **平年与闰年的天数差异**:平年有365天,而闰年因为二月多一天,所以有366天。在代码中,`run`变量用于记录闰年的数量,`ping`则记录平年的数量。 #### 计算每月天数 为了计算出从公元1年开始到输入年份的总天数...

    万年历C++算法的实现

    为了快速定位到每个月的第一天是星期几,我们可以利用蔡勒公式(Zeller's congruence),这是一个计算任意日期对应星期几的公式。 在C++中,可以先编写一个函数来判断是否为闰年,然后根据这个函数来计算每个月的...

    输入公历的某年某月某日,相应的计算出这一天是星期几。

    ### 输入公历的某年某月某日,计算这一天是星期几 在计算机科学与算法领域,计算公历中特定日期对应的星期是一项基础且实用的功能。本篇文章将深入探讨如何利用蔡勒公式来实现这一功能,并对代码进行详细解读。 ##...

    C语言: 输入任意年月日,求该天是星期几

    在C语言中,计算任意给定日期对应的星期几是一个经典的编程问题,这通常涉及到日期处理和模运算。这个问题可以通过使用蔡勒(Zeller's)公式来解决,这是一个在计算机科学中广泛使用的算法。下面我们将深入探讨如何用...

    C++,日历1900年1月1日星期一

    - 考虑到1900年1月1日是星期一,通过累加非闰年和平年天数以及闰年额外的一天,最终求出天数差对7取模得到一周中的哪一天。 ##### 2.3 计算月份第一天是一周中的哪一天 此函数用于计算给定年份和月份的第一天是一周...

Global site tag (gtag.js) - Google Analytics