C語言根據日期判斷星期幾(使用基姆拉爾森計算公式)

2021-10-11 01:53:59 字數 653 閱讀 4551

c語言根據日期判斷星期幾(使用基姆拉爾森計算公式)

演算法如下:

基姆拉爾森計算公式

w= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7

在公式中d表示日期中的日數,m表示月份數,y表示年數。

注意:在公式中有個與其他公式不同的地方:

程式如下:

#include "stdio.h"

void caculateweekday(int y,int m, int d)

int iweek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;

switch(iweek)

}void main()

}執行效果:

請輸入日期:

格式為:1900,1,1

2008,4,29

星期二輸入0退出,其他繼續:d

2008,1,1

星期二輸入0退出,其他繼續:l

2008,8,8

星期五輸入0退出,其他繼續:0

請按任意鍵繼續. . .

編者注:用來算現在真實日期的星期是沒有問題的。原理是根據已知公元1年1月1日的星期數來推算。如果在你的題目中約定了某天是星期幾,你要注意那天的星期是否跟真實的星期相同,如果不同,需要考慮相差幾天!

C 根據日期判斷是否本週,根據日期,獲得星期幾

判斷選擇的日期是否是本週 public static bool isthisweek datetime somedate else 根據日期,獲得星期幾 年 月 日 星期幾,1代表星期一 7代表星期日 public static int getweekday int y,int m,int d 計算...

根據日期顯示星期幾

第1種方法 private string weekday date.text datetime.today.date.tostring yyyy年mm月dd日 int dayofweek int datetime.now.dayofweek week.text weekday dayofweek 第...

根據已知日期計算星期幾

private string weekarrays param date format is 2012 9 18 return weekday private string getweekday final string date int year 0 int month 0 int day 0 t...