C# 以年、日、时、秒为单位的生命时间,独立计算闰年

C# 以年、日、时、秒为单位的生命时间,独立计算闰年,c#,datetime,logic,timespan,C#,Datetime,Logic,Timespan,我目前正在做一个学校项目,我已经达到了要求,但我想挑战自己。我如何根据今天的日期/时间以以下格式准确显示我的年龄 年龄:27岁或27天或27小时或27秒(考虑闰年) 我所做的研究: 我更喜欢寻找它背后的数学。这是我目前使用的数学,但它只精确到16小时或960分钟或57600秒 // Tried using "double" datatype, still same problem. int years = DateTime.Now.Year - dateBirthDate.Year; int d

我目前正在做一个学校项目,我已经达到了要求,但我想挑战自己。我如何根据今天的日期/时间以以下格式准确显示我的年龄

年龄:27岁或27天或27小时或27秒(考虑闰年)

我所做的研究:

我更喜欢寻找它背后的数学。这是我目前使用的数学,但它只精确到16小时或960分钟或57600秒

// Tried using "double" datatype, still same problem.
int years = DateTime.Now.Year - dateBirthDate.Year;
int days = (years / 4) + (years * 365);
int hours = (days * 24) + DateTime.Now.Hour;
int minutes = hours * 60;
int seconds = (minutes * 60) + ((DateTime.Now.Hour * 60) * 60) + DateTime.Now.Second;
应该显示接近0

输出:

Thank you Mat, what is your date of birth? Feel free to include the time you were born. 09/08/2018 5:11pm
Years   :0
Days    :0
Minutes :1020
Seconds :122425
#更新#1 我已经设法使代码部分正常工作,但发现了另一个无法预见的问题。现在,它将不考虑尚未到来的生日。想法

//Needed casting so I could remove the decimals.
TimeSpan span = DateTime.Now.Subtract(dateBirthDate);
int years = (int)span.Days/365;
int months = years * 12;
int days = (int)span.TotalDays;
int hours = (int)span.TotalHours;
int minutes = (int)span.TotalMinutes;
int seconds = (int)span.TotalSeconds;
不可靠的解决办法
必须将TimeSpan强制转换为int以删除小数。为了获取时间跨度年,我只需将月份除以365,然后将其转换为一个(整数),这样它将只显示整数。然后,我创建了一个if/else条件和一个嵌套条件,以调整当前正在发生或尚未到来的生日。逻辑似乎是合理的

        //Needed casting so I could remove the decimals.
        TimeSpan span = DateTime.Now.Subtract(dateBirthDate);

        //Creating workable if/else to account for birthday's yet to come.
        int dateCorrectorMonthNow = DateTime.Now.Month;
        int dateCorrectorDayNow = DateTime.Now.Day;
        int dateCorrectorMonthThen = dateBirthDate.Month;
        int dateCorrectorDayThen = dateBirthDate.Day;


        int years = (int)span.Days/365;
        int months = years * 12;
        int days = (int)span.TotalDays;
        int hours = (int)span.TotalHours;
        int minutes = (int)span.TotalMinutes;
        int seconds = (int)span.TotalSeconds;

        if (dateCorrectorMonthNow <= dateCorrectorMonthThen)
        {
            if (dateCorrectorDayNow  <= dateCorrectorDayThen)
            {
                Console.WriteLine($"Years   :{years}");
            }
            else
            {
                Console.WriteLine($"Years   :{years-1}");
            }
        }
        else
        {
            Console.WriteLine($"Years   :{years}");
        }
//需要强制转换才能删除小数。
TimeSpan=DateTime.Now.Subtract(dateBirthDate);
//创建可行的if/else来解释生日尚未到来。
int dateCorrectorMonthNow=DateTime.Now.Month;
int dateCorrectorDayNow=DateTime.Now.Day;
int datecorrectormonthen=dateBirthDate.Month;
int dateCorrectorDayThen=dateBirthDate.Day;
整数年=(整数)span.Days/365;
整数个月=年*12;
整数天=(整数)span.TotalDays;
整数小时=(整数)span.TotalHours;
int minutes=(int)span.TotalMinutes;
int秒=(int)span.TotalSeconds;

如果(DateCorrector Month现在我觉得我的闰年数学计算方法也不可靠。假设我们在闰年着陆,那么数学计算也会不准确。这里有一个有趣的通读,你会想为此创建一个单元测试,需要两个日期并进行计算。使用一些数据进行单独计算像Excel这样的方法来计算每个日期对的预期结果。你需要考虑2月29日出生的人(或者在2月29日做计算)。你还需要考虑在FEB,2100(不是闰年)结束时的日期对。.做这样的日期计算会让大脑负担过重。当你完成后,写一个算法,计算出每年复活节的时间!@Flydog57我现在暗地里恨你,我想我肯定有,但我100%忘记了闰年生日会发生什么。。。。