[C#] 纯文本查看 复制代码 /// <summary>
/// 根据日期,获取星期几
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public string GetWeek(DateTime time)
{
int y = time.Year;
int m = time.Month;
int d = time.Day;
if (m == 1 || m == 2)
{
m += 12;
y--; //把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
}
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;
}
|