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

算法如下: 
  基姆拉尔森计算公式: W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
  在公式中d表示日期中的日数,m表示月份数,y表示年数。注意:在公式中有个与其他公式不同的地方:
  把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
但是在测试的时候发现有点出入,就是公式存在一点问题,得稍做修改:
 W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 +1

 

 

#region 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
        /// <summary>
        /// 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
        /// </summary>
        /// <param name="y"></param>
        /// <param name="m"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string CaculateWeekDay(int y,int m, int d)
        {
            if(m==1) m=13;
            if(m==2) m=14;
            int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7+1; 
            string weekstr="";
            switch(week)
            {
                case 1: weekstr="星期一"; break;
                case 2: weekstr="星期二"; break;
                case 3: weekstr="星期三"; break;
                case 4: weekstr="星期四"; break;
                case 5: weekstr="星期五"; break;
                case 6: weekstr="星期六"; break;
                case 7: weekstr="星期日"; break;
            }

            return weekstr; 
        }
        #endregion

 

 一周从周日算起   
  DateTime   mDatetime=new   DateTime(year,1,1);//year为要求的那一年   
  int   firstweekfirstday=Convert.ToInt32(mDTime.DayOfWeek);//一年中第一天是周几   
  days=(double)(6-firstweekfirstday);   
  DateTime   secondweekfisrtday=mDTime.AddDays(days);第二周一   
  fisrtday=MDatetime.AddDays(week*7);//第N周第一天   
  lastday=MDatetime.AddDays(week*7+6);//第N周最后一天   
    
  一周从周一算起   
  DateTime   mDatetime=new   DateTime(year,1,1);//year为要求的那一年   
  int   firstweekfirstday=Convert.ToInt32(mDTime.DayOfWeek);//一年中第一天是周几   
  days=(double)(7-firstweekfirstday);   
  DateTime   secondweekfisrtday=mDTime.AddDays(days);第二周一   
  fisrtday=MDatetime.AddDays(week*7);//第N周第一天   
  lastday=MDatetime.AddDays(week*7+6);//第N周最后一天   

javascript:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function showDateCount(year,weekNum){
 var date=new Date(year,0,(7*weekNum));
 var FD=new Date(date.getYear(),(date.getMonth()),(date.getDate()-date.getDay()+1))
 var Monday=FD.getYear()+"-"+(FD.getMonth()+1)+"-"+FD.getDate()//一周的第一天
 
 var LD=new Date(date.getYear(),(date.getMonth()),(date.getDate()-date.getDay()+7))
 var Sunday=LD.getYear()+"-"+(LD.getMonth()+1)+"-"+LD.getDate() //一周的最后一天
 alert(year+"年第"+weekNum+"周是"+Monday+""+Sunday)
}
</SCRIPT>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<input type="text" value="2007" size="20" name="year">年<input type="text" name="weekNum" value="30" size="20">
<input type="button" name="GO" value="Click" onClick="showDateCount(year.value,weekNum.value)">

</BODY>
</HTML>

 c#
//monthCalendar1是日历控件
 System.Globalization.ChineseLunisolarCalendar c = new ChineseLunisolarCalendar();
        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            DateTime MySelectDate = e.End;
            txt_nonglidate.Text = string.Format(CultureInfo.InstalledUICulture, "{0}-{1}-{2}", c.GetYear(MySelectDate), c.GetMonth(MySelectDate), c.GetDayOfMonth(MySelectDate));
        }

 

 

posted on 2013-06-18 12:07  douqiumiao  阅读(408)  评论(0编辑  收藏  举报