解密出生日期----C#编程

题目描述

使用C#编写一个静态方法。该方法能够根据出生日期,(1)计算此人的年龄;(2)计算从现在到其60周岁期间,总共多少天。

输入

一个人的出生日期;

输出

第一行,此人的年龄(只按年度计算)
第二行,此人从现在到其60周岁期间,总共多少天(天数占5位宽度,右对齐)。

样例输入

2019-12-4

样例输出

0
21914

提示

假定现在的日期是2019年12月 5日

熟悉运用C#中的DateTime类型数据以及时间函数!!!!!!

using System;
//using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace lianxi
{
    class Class1
    {
        static void Main(string[] args)
        {
            DateTime a = new DateTime();
            a = Convert.ToDateTime(Console.ReadLine());
            DateTime b = new DateTime(2019, 12, 5);
            TimeSpan c = b - a;
            //TimeSpan c = a - b;    有个坑:为什么不是这样的呢???
            int age = c.Days / 365;
            Console.WriteLine(age);
            a = a.AddYears(60);      //给a的年加60
            c = a - b;
            Console.WriteLine(c.Days);
            Console.ReadKey();
        }
    }
}

 

发布了44 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/DreamTrue1101/article/details/104841949