您好,登錄后才能下訂單哦!
1.判斷閏年
2.通過年和月的確定 判斷月有幾天
3.通過年 月 日 來判斷星期幾 基姆拉爾森計算公式
- using System;
- using System.Text;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Collections;
- namespace 日歷
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack) //該值指示該頁是否正為響應客戶端回發而加載,或者它是否正被首次加載和訪問,如果是為響應客戶端回發而加載該頁,則為true;否則為 false。
- {
- ArrayList arrList1 = new ArrayList();
- for (int i = 2000; i <= 2012; i++)
- {
- arrList1.Add(i);
- }
- year.DataSource = arrList1;
- year.DataBind();
- ArrayList arrList2 = new ArrayList();
- for (int i = 1; i <= 12; i++)
- {
- arrList2.Add(i);
- }
- month.DataSource = arrList2;
- month.DataBind();
- ArrayList arrList3 = new ArrayList();
- for (int i = 1; i <= 31; i++)
- {
- arrList3.Add(i);
- }
- day.DataSource = arrList3;
- day.DataBind();
- }
- }
- protected void day_SelectedIndexChanged(object sender, EventArgs e)
- {
- int[] a = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //將數組作為控件數據源
- int[] b = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int s1 = Int32.Parse(year.Text);
- int s2 = Int32.Parse(month.Text);
- int s3 = Int32.Parse(day.Text);
- if ((s1%4 == 0 && s1%100 != 0) || (s1%400 == 0)) //判斷閏年
- {
- ArrayList arrList3 = new ArrayList();
- for (int i = 1; i <= b[s2-1]; i++) //是閏年 則用b數組中的天數
- {
- arrList3.Add(i);
- }
- day.DataSource = arrList3;
- day.DataBind();
- }
- else
- {
- ArrayList arrList3 = new ArrayList();
- for (int i = 1; i <= a[s2 - 1]; i++) //不是閏年 則用a數組中的天數
- {
- arrList3.Add(i);
- }
- day.DataSource = arrList3;
- day.DataBind();
- }
- xinqi.Text = caculateweekday(s1, s2, s3);
- }
- String caculateweekday(int y, int m, int d) //計算星期幾
- {
- if (m == 1) { m = 13; y--; }
- if (m == 2) { m = 14; y--; }
- 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;
- }
- protected void zz(object sender, EventArgs e) //改變天數時 改變星期數 我覺得這里可以優化
- {
- int s1 = Int32.Parse(year.Text);
- int s2 = Int32.Parse(month.Text);
- int s3 = Int32.Parse(day.Text);
- xinqi.Text = caculateweekday(s1, s2, s3);
- }
- }
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。