# DIY 跟据日期计算星期几

—— 蔡勒（Zeller）公式

=49+[49/4]+[20/4]-2×20+[26× (10+1)/10]+1-1
=49+[12.25]+5-40+[28.6]

<span style="font-family: tahoma, 宋体; line-height: 21.3333px; background-color: rgb(250, 250, 252);">=49+12+5-40+28</span>
=54 (除以7余5)

/*

* 这里通过蔡勒公式算出某一天是星期几, C#
*/

        private String GetWeek(String _date)
{
DateTime tdate = Convert.ToDateTime(_date);
String myWeek = null;

int y = int.Parse(("00" + tdate.Year.ToString()).Substring(tdate.Year.ToString().Length, 2));
int m = tdate.Month > 2 ? tdate.Month : tdate.Month+12;
int c = (tdate.Year / 100);
int d = tdate.Day;
int w = (y + (y / 4) + (c / 4) - 2 * c + (26 * (m + 1) / 10) + d - 1) % 7;
switch (w)
{
case 0:
myWeek = "日";
break;
case 1:
myWeek = "一";
break;
case 2:
myWeek = "二";
break;
case 3:
myWeek = "三";
break;
case 4:
myWeek = "四";
break;
case 5:
myWeek = "五";
break;
case 6:
myWeek = "六";
break;
default:
break;
}

myWeek = tdate.Year + "年" + tdate.Month + "月" + tdate.Day + "日" + "周" + myWeek;
return myWeek;
}

VB:

    Private Function GetWeek(ByVal _date As String) As String
Dim tdate As DateTime = Convert.ToDateTime(_date)
Dim myWeek As String = String.Empty

Dim y As Integer = CUInt(("00" + tdate.Year.ToString()).Substring(tdate.Year.ToString().Length, 2))
Dim m As Integer = IIf(tdate.Month > 2, tdate.Month, tdate.Month + 12)
Dim c As Integer = (tdate.Year \ 100)
Dim d As Integer = tdate.Day
Dim w As Integer = (y + (y \ 4) + (c \ 4) - 2 * c + (26 * (m + 1) \ 10) + d - 1) Mod 7

Select w
Case 0
myWeek = "日"
Case 1
myWeek = "一"
Case 2
myWeek = "二"
Case 3
myWeek = "三"
Case 4
myWeek = "四"
Case 5
myWeek = "五"
Case 6
myWeek = "六"
End Select

myWeek = tdate.Year.ToString + "年" + tdate.Month.ToString + "月" + tdate.Day.ToString + "日" + "周" + myWeek
Return myWeek
End Function

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
09-10

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。