曜日と日数の計算

事件が発生した日付をタイプして、曜日と本日までの経過日数を計算します。
日付 経過日数 曜日 説明
1/1/1 738202 西暦元年元旦
1995/1/17 9893 阪神淡路大震災
1997/8/31 8936 ダイアナ妃交通事故死
2001/9/11 7464 アメリカ航空機テロ
2011/3/11 3996 東日本大震災

CLI で動かす

  1. CLIYoubi.cs の名前で utf-8(BOM 有り)でタイプして C:\DATA\C#\BAT\ に格納して下さい。
    /*★ 西暦1年1月1日からの通算日数を計算     前田 稔 ★*/
    using System;
    
    class Text
    {
        static int[]    MTBL= { 0,31,59,90,120,151,181,212,243,273,304,334 };
        static string[] WTBL= { "日","月","火","水","木","金","土" };
        static int      yy,mm,dd;
    
        // Main()
        public static void Main()
        {   int     today,count,week;
    
            //西暦1年1月1日から今日までの日数を調べる
            yy= DateTime.Today.Year;
            mm= DateTime.Today.Month;
            dd= DateTime.Today.Day;
            today= days();
            GetDate();
            count= days();
            week= count%7;
            count= today-count+1;
            Console.WriteLine("{0}年 {1}月 {2}日 ({3}曜日)  通算日数={4}",
                    yy, mm, dd, WTBL[week], count);
            Console.ReadLine();
        }
        // 生年月日を入力して yy,mm,dd に設定
        static void GetDate()
        {
            string str;
            string[] ymd;
            Console.WriteLine("年月日= YYYY/MM/DD");
            str = Console.ReadLine();
            ymd= str.Split(new char[] {'/'});
            yy= int.Parse(ymd[0]);
            mm= int.Parse(ymd[1]);
            dd= int.Parse(ymd[2]);
        }
        // 西暦1年1月1日からの通算日数を調べる関数
        static int  days()
        {   int     w;
    
            w=  (yy-1)*365;         //年*365
            w+= (yy/4 - yy/100 + yy/400 + MTBL[mm-1] + dd);
            if (mm<3 && uruu(yy))  w--;
            return(w);
        }
        // 閏年を調べる関数
        static bool  uruu(int year)
        {   if (year%400L==0L)  return(true);
            if (year%100L==0L)  return(false);
            if (year%4L==0L)    return(true);
            return(false);
        }
    }
    
  2. Windows10 のスタートアイコンから[Microsoft Visual Studio 2005][Visual Studio 2005 コマンド プロンプト] を起動します。
    cd コマンドで C:\DATA\C#\BAT\ のフォルダーに移動します。
    csc コマンドでコンパイルします。
    *.exe を実行します。
  3. MTBL は大の月と小の月の日数を累計した配列です。
    WTBL は曜日を印字するための配列です。
        static int[]    MTBL= { 0,31,59,90,120,151,181,212,243,273,304,334 };
        static string[] WTBL= { "日","月","火","水","木","金","土" };
    
  4. GetDate() 関数で、年月日を入力して / で切り分けて yy,mm,dd 設定します。
        static void GetDate()
        {
            string str;
            string[] ymd;
            Console.WriteLine("年月日= YYYY/MM/DD");
            str = Console.ReadLine();
            ymd= str.Split(new char[] {'/'});
            yy= int.Parse(ymd[0]);
            mm= int.Parse(ymd[1]);
            dd= int.Parse(ymd[2]);
        }
    
  5. 西暦1年1月1日からの通算日数を調べる関数です。
        // 西暦1年1月1日からの通算日数を調べる関数
        static int  days()
        {   int     w;
    
            w=  (yy-1)*365;         //年*365
            w+= (yy/4 - yy/100 + yy/400 + MTBL[mm-1] + dd);
            if (mm<3 && uruu(yy))  w--;
            return(w);
        }
    
  6. 閏年を調べる関数です。
        static bool  uruu(int year)
        {   if (year%400L==0L)  return(true);
            if (year%100L==0L)  return(false);
            if (year%4L==0L)    return(true);
            return(false);
        }
    

超初心者のプログラム入門(C#)