c# 根据日期获取星期方法

发布时间:2011年05月09日      浏览次数:1383 次
protected void Page_Load(object sender, EventArgs e)
{
int m = System.DateTime.Today.Month;
int y = System.DateTime.Today.Year;
int d = System.DateTime.Today.Day;
int weeks = getWeekDay(y, m, d);
switch (weeks)
{
case 1:
this.TextBox1.Text = "星期一";
break;
case 2:
this.TextBox1.Text = "星期二";
break;
case 3:
this.TextBox1.Text = "星期三";
break;
case 4:
this.TextBox1.Text = "星期四";
break;
case 5:
this.TextBox1.Text = "星期五";
break;
case 6:
this.TextBox1.Text = "星期六";
break;
case 7:
this.TextBox1.Text = "星期日";
break;
}


}


/// <summary>根据日期,获得星期几</summary>
/// <param name="y">年</param>
/// <param name="m">月</param>
/// <param name="d">日</param>
/// <returns>星期几,1代表星期一;7代表星期日</returns>
public static int getWeekDay(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;
return week;
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!