以年、日、小时、秒计的独立计算闰年的时间
Time alive in Years, Days, Hours, Seconds Independently Accounting for Leap Year
我目前正在做一个我已经达到要求的学校项目,但我想挑战自己。如何根据今天的date/time?
以下列格式准确显示我的年龄
年龄:27 岁 - 或 - 天 - 或 - 时 - 或 - 秒(考虑闰年)
我所做的研究:How would you calculate the age in C# using date of birth (considering leap years)
我更想寻找它背后的数学原理。这是我目前使用的数学,但它只精确到 16 小时或 960 分钟或 57,600 秒。
// 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 以删除小数。为了获取 TimeSpan 年份,我简单地用月份除以 365,然后将其转换为 (int),以便它只显示整数。然后我创建了一个 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}");
}
我目前正在做一个我已经达到要求的学校项目,但我想挑战自己。如何根据今天的date/time?
以下列格式准确显示我的年龄年龄:27 岁 - 或 - 天 - 或 - 时 - 或 - 秒(考虑闰年)
我所做的研究:How would you calculate the age in C# using date of birth (considering leap years)
我更想寻找它背后的数学原理。这是我目前使用的数学,但它只精确到 16 小时或 960 分钟或 57,600 秒。
// 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 以删除小数。为了获取 TimeSpan 年份,我简单地用月份除以 365,然后将其转换为 (int),以便它只显示整数。然后我创建了一个 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}");
}