码迷,mamicode.com
首页 > 其他好文 > 详细

课后作业 04 --DateTime应用,判断多久后生日之类

时间:2015-08-07 23:54:48      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

           try
            {
                Console.Write("请以年-月-日的形式输入您的生日:");
                string strA = Console.ReadLine();
                DateTime bir = Convert.ToDateTime(strA);
                DateTime tod = DateTime.Today;
                strA = bir.ToString("MM-dd");//将输入生日转换为月 日格式
                string strB = tod.ToString("MM-dd");//今天月 日
                bir = DateTime.Parse(strA);//再将生日字符串转换成日期
                tod = DateTime.Parse(strB);
                TimeSpan ti = bir - tod;//时间间隔
                int j = ti.Days,k;
                if (j >= 0)
                {
                    if (j == 0)
                        Console.WriteLine("您今天生日,生日快乐!");
                    else
                    Console.WriteLine("您的生日距今还有"+j+"天");//还有多久生日
                }
                else
                {
                    DateTime tod2 = tod.AddYears(1);
                    TimeSpan t2 = tod2 - tod;
                    k = t2.Days;
                    bir = bir.AddYears(1);
                    //TimeSpan t3 = tod2 - bir;
                    //k = t2.Days - t3.Days;//第二种判断方法
                    Console.WriteLine("您今年生日已过,明年生日距今还有"+ (k+j) +"天");//明年生日,还有多久
                    
                }

                Console.WriteLine("您的生日是在" + bir.DayOfWeek + "," +"是生日当年的第"+ bir.DayOfYear +"天");
            }
            catch
            {
                Console.WriteLine("您输入的格式有误!");
            }
            Console.ReadLine();

  

课后作业 04 --DateTime应用,判断多久后生日之类

标签:

原文地址:http://www.cnblogs.com/whytohow/p/4712247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!