Формат A TimeSpan с годами
У меня есть класс с двумя свойствами даты: FirstDay
и LastDay
. LastDay
является нулевым. Я хотел бы сгенерировать строку в формате "x year(s) y day(s)"
. Если всего лет меньше 1, я хотел бы опустить раздел года. Если общее количество дней меньше 1, я бы хотел пропустить раздел дня. Если ни годы, ни дни равны 0, они должны сказать "день/год", а не "дни/годы" соответственно.
<сильные > Примеры:
2,2 года: "2 года 73 дня"
1.002738 лет: "1 год 1 день"
0,2 года:
2 года:
У меня есть работы, но они длинны:
private const decimal DaysInAYear = 365.242M;
public string LengthInYearsAndDays
{
get
{
var lastDay = this.LastDay ?? DateTime.Today;
var lengthValue = lastDay - this.FirstDay;
var builder = new StringBuilder();
var totalDays = (decimal)lengthValue.TotalDays;
var totalYears = totalDays / DaysInAYear;
var years = (int)Math.Floor(totalYears);
totalDays -= (years * DaysInAYear);
var days = (int)Math.Floor(totalDays);
Func<int, string> sIfPlural = value =>
value > 1 ? "s" : string.Empty;
if (years > 0)
{
builder.AppendFormat(
CultureInfo.InvariantCulture,
"{0} year{1}",
years,
sIfPlural(years));
if (days > 0)
{
builder.Append(" ");
}
}
if (days > 0)
{
builder.AppendFormat(
CultureInfo.InvariantCulture,
"{0} day{1}",
days,
sIfPlural(days));
}
var length = builder.ToString();
return length;
}
}
Есть ли более краткий способ сделать это (но все еще читаемый)?
Ответы
Ответ 1
A TimeSpan
не имеет разумной концепции "лет", потому что это зависит от начальной и конечной точки. (Месяцы похожи - сколько месяцев в течение 29 дней? Ну, это зависит...)
Чтобы дать бесстыдную плагин, мой проект Noda Time делает это очень простым:
using System;
using NodaTime;
public class Test
{
static void Main(string[] args)
{
LocalDate start = new LocalDate(2010, 6, 19);
LocalDate end = new LocalDate(2013, 4, 11);
Period period = Period.Between(start, end,
PeriodUnits.Years | PeriodUnits.Days);
Console.WriteLine("Between {0} and {1} are {2} years and {3} days",
start, end, period.Years, period.Days);
}
}
Вывод:
Between 19 June 2010 and 11 April 2013 are 2 years and 296 days
Ответ 2
public string GetAgeText(DateTime birthDate)
{
const double ApproxDaysPerMonth = 30.4375;
const double ApproxDaysPerYear = 365.25;
/*
The above are the average days per month/year over a normal 4 year period
We use these approximations as they are more accurate for the next century or so
After that you may want to switch over to these 400 year approximations
ApproxDaysPerMonth = 30.436875
ApproxDaysPerYear = 365.2425
How to get theese numbers:
The are 365 days in a year, unless it is a leepyear.
Leepyear is every forth year if Year % 4 = 0
unless year % 100 == 1
unless if year % 400 == 0 then it is a leep year.
This gives us 97 leep years in 400 years.
So 400 * 365 + 97 = 146097 days.
146097 / 400 = 365.2425
146097 / 400 / 12 = 30,436875
Due to the nature of the leap year calculation, on this side of the year 2100
you can assume every 4th year is a leap year and use the other approximatiotions
*/
//Calculate the span in days
int iDays = (DateTime.Now - birthDate).Days;
//Calculate years as an integer division
int iYear = (int)(iDays / ApproxDaysPerYear);
//Decrease remaing days
iDays -= (int)(iYear * ApproxDaysPerYear);
//Calculate months as an integer division
int iMonths = (int)(iDays / ApproxDaysPerMonth);
//Decrease remaing days
iDays -= (int)(iMonths * ApproxDaysPerMonth);
//Return the result as an string
return string.Format("{0} years, {1} months, {2} days", iYear, iMonths, iDays);
}
Ответ 3
Я бы не сделал этого с помощью TimeSpan
. Математика даты становится сложной, как только вы выходите за пределы дней, потому что количество дней в месяц и дни в году больше не является постоянным. Вероятно, почему TimeSpan
не содержит свойств для Years
и Months
. Вместо этого я определял бы количество лет/месяцев/дней и т.д. Между двумя значениями DateTime
и соответствующим образом отображал результаты.
Ответ 4
Я думаю, что это должно сработать:
public static int DiffYears(DateTime dateValue1, DateTime dateValue2)
{
var intToCompare1 = Convert.ToInt32(dateValue1.ToString("yyyyMMdd"));
var intToCompare2 = Convert.ToInt32(dateValue2.ToString("yyyyMMdd"));
return (intToCompare2 - intToCompare1) / 10000;
}
Ответ 5
Public Function TimeYMDBetween(StartDate As DateTime, EndDate As DateTime) As String
Dim Years As Integer = EndDate.Year - StartDate.Year
Dim Months As Integer = EndDate.Month - StartDate.Month
Dim Days As Integer = EndDate.Day - StartDate.Day
Dim DaysLastMonth As Integer
'figure out how many days were in last month
If EndDate.Month = 1 Then
DaysLastMonth = DateTime.DaysInMonth(EndDate.Year - 1, 12)
Else
DaysLastMonth = DateTime.DaysInMonth(EndDate.Year, EndDate.Month - 1)
End If
'adjust for negative days
If Days < 0 Then
Months = Months - 1
Days = Days + DaysLastMonth 'borrowing from last month
End If
'adjust for negative Months
If Months < 0 Then 'startdate hasn't happend this year yet
Years = Years - 1
Months = Months + 12
End If
Return Years.ToString() + " Years, " + Months.ToString() + " Months and " + Days.ToString() + " Days"
End Function