zoukankan      html  css  js  c++  java
  • 课后作业 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();
    

      

  • 相关阅读:
    概率论
    计算机网络基础
    数据库 数据库管理系统 数据库系统
    第二次冲刺总结
    Beta版总结会议
    Beta阶段项目总结
    beta版本“足够好”/测试矩阵
    zencart批量更新后台邮箱地址sql
    php首页定向到内页代码
    用.htaccess 禁止IP访问
  • 原文地址:https://www.cnblogs.com/whytohow/p/4712247.html
Copyright © 2011-2022 走看看