A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张荣耀 中级黑马   /  2013-9-5 00:05  /  2415 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,并且计算出是一年中的第几天

4 个回复

倒序浏览
protected void Button1_Click(object sender, EventArgs e)
{

        string str = this.TextBox1.Text;
        string year = str.Substring(0, 4);
        string month = str.Substring(5, 2);
        string Day = str.Substring(8, 2);
        if (month.Substring(0, 1) == "0")
        {
            month = month.Substring(1, 1);
        }
        if (Day.Substring(0, 1) == "0")
        {
            Day = Day.Substring(1, 1);
        }
        this.TextBox1.Text = year + "年" + month + "月" + Day + "日";

        //this.TextBox1.Text = year + "-" + month + "-" + Day + "-";}
回复 使用道具 举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test10
{
    class Program
    {
        //编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,
        //程序计算这一天是星期几,并且计算出是一年中的第几天。
        static bool yearflag;
        static void Main(string[] args)
        {
            do
            {
                Console.WriteLine("按照" + "yyyy-MM-dd" + "的格式输入一个日期");
                string date = Console.ReadLine();
                string[] sdate = date.Split('-');
                if (sdate.Length != 3)
                {
                    Console.WriteLine("日期不合法");
                    continue;
                }
                //年份
                if (Isint(sdate[0]) == 0)
                {
                    Console.WriteLine("年份不合法");
                    continue;
                }
                if (Isint(sdate[0]) % 400 == 0 || Isint(sdate[0]) % 4 == 0 && Isint(sdate[0]) % 100 != 0)
                {
                   yearflag = true;
                }
                else
                {
                    yearflag = false;
                }
                //月份
                if ( Isint(sdate[1]) < 1 || Isint(sdate[1]) > 12)
                {
                    Console.WriteLine("月份不合法");
                    continue;
                }
                //天数
                if (Isint(sdate[2]) == 0)
                {
                    Console.WriteLine("天数不合法");
                    continue;
                }
                if (Isint(sdate[2]) < 1 || Isint(sdate[2]) > 31)
                {
                    Console.WriteLine("天数应在1-31天之间");
                    continue;
                }
                else
                {
                    if (Isint(sdate[1]) == 4 || Isint(sdate[1]) == 6 ||
                        Isint(sdate[1]) == 9 || Isint(sdate[1]) == 11)
                    {
                        if (Isint(sdate[2]) > 30)
                        {
                            Console.WriteLine("该月天数不等大于30天");
                            continue;
                        }
                    }
                    if (Isint(sdate[1]) == 2)
                    {
                        if (yearflag)
                        {
                            if (Isint(sdate[2]) > 29)
                            {
                                Console.WriteLine("闰年,2月天数不能大于29天");
                                continue;
                            }
                        }
                        else
                        {
                            if (Isint(sdate[2]) > 28)
                            {
                                Console.WriteLine("平年,2月天数不能大于28天");
                                continue;
                            }
                        }
                    }
                }

                Console.WriteLine("该日期是:"+ GetWeekDay(Isint(sdate[0]), Isint(sdate[1]), Isint(sdate[2])));
                Console.WriteLine("该日期是第" + Countday(Isint(sdate[1])) + "天");
                Console.ReadKey();
            } while (true);
         }

        //计算出是一年中的第几天。
        static int Countday(int date)
        {
            int d = 0;
            yearflag = !yearflag;
            if (date == 1)
            {
                d = date;
            }
            else if (date == 2)
            {
                d = 31 + date;
            }
            else if (date == 3)
            {
                if (yearflag)
                {
                    d = 31 + 28 + date;
                }
                else
                {
                    d = 31 + 29 + date;
                }
            }
            else if (date == 4)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + date;
                }
            }
            else if (date == 5)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + date;
                }
            }
            else if (date == 6)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + 31 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + 31 + date;
                }
            }
            else if (date == 7)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + 31 + 30 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + 31 + 30 + date;
                }
            }
            else if (date == 8)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date;
                }
            }
            else if (date == 9)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date;
                }
            }
            else if (date == 10)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;
                }
            }
            else if (date == 11)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;
                }
            }
            else if (date == 12)
            {
                if (yearflag)
                {
                    d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
                }
                else
                {
                    d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;
                }
            }
            return d;
        }
        //程序计算这一天是星期几
        static string GetWeekDay(int y, int m, int d)
        {
            if (m == 1 || m == 2)
            {
                m += 12;
                y--; //把一月和二月看成是上一年的十三月和十四月  
            }
            // W = (Y-1) + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + [ 13 * (M+1) / 5 ] + d.
            int week = (y - 1 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + 13 * (m + 1) / 5 + d )%7;
            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 0: weekstr = "星期日"; break;
            }
            return weekstr;
        }
       //时间是否合法
        static int Isint(string date)
        {
            try
           {
                 int aa = Convert.ToInt32(date);
                 if (aa < 1)
                 {
                    Console.WriteLine("日期不合法");
                    return 0;
                 }
                 else
                 {
                     return aa;
                 }
            }
           catch
            {
                Console.WriteLine("日期不合法");
                return 0;
            }
        }
    }
}

评分

参与人数 1技术分 +2 收起 理由
赵宗荣 + 2 赞一个!

查看全部评分

回复 使用道具 举报
  1. static void Main(string[] args)
  2.         {
  3.             //编写程序,该程序启动后用户可以按“yyyy-MM-dd”的格式输入一个日期,程序计算这一天是星期几,并且计算出是一年中的第几天
  4.             while (true)
  5.             {
  6.                 Console.WriteLine("请输入“yyyy-MM-dd”的格式输入一个日期");
  7.                 DateTime newdate;
  8.                 string date = Console.ReadLine();
  9.                 if (DateTime.TryParse(date, out newdate))
  10.                 {
  11.                     int day = newdate.DayOfYear;
  12.                     var week = newdate.DayOfWeek;
  13.                     Console.WriteLine("是今年的第"+day + "天,今天是 " + week);
  14.                     break;
  15.                 }
  16.                 else
  17.                 {
  18.                     Console.WriteLine("输入的日期格式不正确,请重新输入");
  19.                 }
  20.             }
  21.          
  22.             Console.ReadKey();
  23.         }
复制代码
回复 使用道具 举报
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;

  6. namespace ConsoleApplication7
  7. {
  8.     class Program
  9.     {
  10.         public static int GetYear(string str)
  11.         {
  12.             List<int> list = new List<int>();
  13.             MatchCollection matches = Regex.Matches(str, @"\d+");
  14.             foreach (Match item in matches)
  15.             {
  16.                 list.Add(Convert.ToInt32(item.Value));
  17.             }
  18.             return list[0];
  19.         }
  20.         public static int GetMonth(string str)
  21.         {
  22.             List<int> list = new List<int>();
  23.             MatchCollection matches = Regex.Matches(str, @"\d+");
  24.             foreach (Match item in matches)
  25.             {
  26.                 list.Add(Convert.ToInt32(item.Value));
  27.             }
  28.             return list[1];
  29.         }
  30.         public static int GetDay(string str)
  31.         {
  32.             List<int> list = new List<int>();
  33.             MatchCollection matches = Regex.Matches(str, @"\d+");
  34.             foreach (Match item in matches)
  35.             {
  36.                 list.Add(Convert.ToInt32(item.Value));
  37.             }
  38.             return list[2];
  39.         }
  40.         public static bool IsYear(string str)
  41.         {
  42.             int year = GetYear(str);
  43.             if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
  44.             {
  45.                 return true;
  46.             }
  47.             return false;
  48.         }
  49.         public static string GetWeek(int day)
  50.         {
  51.             Dictionary<int, string> dictionary = new Dictionary<int, string>();
  52.             dictionary.Add(0, "星期一");
  53.             dictionary.Add(1, "星期二");
  54.             dictionary.Add(2, "星期三");
  55.             dictionary.Add(3, "星期四");
  56.             dictionary.Add(4, "星期五");
  57.             dictionary.Add(5, "星期六");
  58.             dictionary.Add(6, "星期日");
  59.             if (dictionary.ContainsKey(day))
  60.             {
  61.             return dictionary[day];
  62.             }
  63.             return "未知的日期";
  64.         }
  65.         static void Main(string[] args)
  66.         {   
  67.             string str = "2013-09-04";
  68.             bool flag = IsYear(str);
  69.             int days = GetDay(str);
  70.             int month=GetMonth(str);
  71.             for (int i = 1; i < month; i++)
  72.             {
  73.                 switch (i)
  74.                 {
  75.                     case 1:
  76.                     case 3:
  77.                     case 5:
  78.                     case 7:
  79.                     case 8:
  80.                     case 10:
  81.                     case 12:
  82.                         days = days + 31;
  83.                         break;
  84.                     case 2:
  85.                         if (flag == true)
  86.                         {
  87.                             days = days + 29;
  88.                         }
  89.                         else
  90.                         {
  91.                             days = days + 28;
  92.                         }
  93.                         break;
  94.                     case 4:
  95.                     case 6:
  96.                     case 9:
  97.                     case 11:
  98.                         days = days + 30;
  99.                         break;
  100.                     default:
  101.                         break;
  102.                 }
  103.             }
  104.             string week = GetWeek(days % 7);
  105.             Console.WriteLine("{0}是一年中的第{1}天,这一天是{2}",str,days,week);
  106.             Console.ReadKey();
  107.         }
  108.     }
  109. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马