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

           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();

  

转载于:https://www.cnblogs.com/whytohow/p/4712247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值