Calculate Years, Months, weeks and Days
In my application, a user enters two dates. A scheduled start date, and a scheduled end date. We have to take those dates, and populate 4 fields, based on the difference.
So, lets sa开发者_开发问答y he selects 1st Jan, 2010 as a start, and 2nd of March, 2011 as the end, we need to end up with:
Years: 1 Months: 2 Weeks: 0 Days 1
Meaning the total duration is 1 year, 2 months and 1 day.
Is there a standard way of doing this? Or would I need to write a method that has a lot of pretty tricky logic to work it out? I was hoping I'd be lucky, and there would be a date-diff type .Net class available.
Heres a complete method, weeks are not included, but could be added relatively simply. It's a somewhat complex question (asked in a multitude of ways on stackoverflow and answered poorly in a multitude of ways), but none the less can be answered. The TimeSpan object gives us part of what we need, but only works up through days. I've written a significant number of tests against this method, if you find a hole, please post a comment.
What this will do is compare 2 dates, getting the years, months, days, hours, and minutes. (e.g. some event happened 1 year, 6 months, 3 days, 4 hours and 7 minutes ago)
Because this question has been asked and attempted to be answered so many times, I'm not sure this will ever even get noticed, but if so it should provide value.
public static void TimeSpanToDateParts(DateTime d1, DateTime d2, out int years, out int months, out int days, out int hours, out int minutes)
{
if (d1 < d2)
{
var d3 = d2;
d2 = d1;
d1 = d3;
}
var span = d1 - d2;
months = 12 * (d1.Year - d2.Year) + (d1.Month - d2.Month);
//month may need to be decremented because the above calculates the ceiling of the months, not the floor.
//to do so we increase d2 by the same number of months and compare.
//(500ms fudge factor because datetimes are not precise enough to compare exactly)
if (d1.CompareTo(d2.AddMonths(months).AddMilliseconds(-500)) <= 0)
{
--months;
}
years = months / 12;
months -= years * 12;
if (months == 0 && years == 0)
{
days = span.Days;
}
else
{
var md1 = new DateTime(d1.Year, d1.Month, d1.Day);
// Fixed to use d2.Day instead of d1.Day
var md2 = new DateTime(d2.Year, d2.Month, d2.Day);
var mDays = (int) (md1 - md2).TotalDays;
if (mDays > span.Days)
{
mDays = (int)(md1.AddMonths(-1) - md2).TotalDays;
}
days = span.Days - mDays;
}
hours = span.Hours;
minutes = span.Minutes;
}
You can use the DateDiff class of this free library:
// ----------------------------------------------------------------------
public void DateDiffSample()
{
DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
Console.WriteLine( "Date1: {0}", date1 );
// > Date1: 08.11.2009 07:13:59
DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
Console.WriteLine( "Date2: {0}", date2 );
// > Date2: 20.03.2011 19:55:28
DateDiff dateDiff = new DateDiff( date1, date2 );
// differences
Console.WriteLine( "DateDiff.Years: {0}", dateDiff.Years );
// > DateDiff.Years: 1
Console.WriteLine( "DateDiff.Quarters: {0}", dateDiff.Quarters );
// > DateDiff.Quarters: 5
Console.WriteLine( "DateDiff.Months: {0}", dateDiff.Months );
// > DateDiff.Months: 16
Console.WriteLine( "DateDiff.Weeks: {0}", dateDiff.Weeks );
// > DateDiff.Weeks: 70
Console.WriteLine( "DateDiff.Days: {0}", dateDiff.Days );
// > DateDiff.Days: 497
Console.WriteLine( "DateDiff.Weekdays: {0}", dateDiff.Weekdays );
// > DateDiff.Weekdays: 71
Console.WriteLine( "DateDiff.Hours: {0}", dateDiff.Hours );
// > DateDiff.Hours: 11940
Console.WriteLine( "DateDiff.Minutes: {0}", dateDiff.Minutes );
// > DateDiff.Minutes: 716441
Console.WriteLine( "DateDiff.Seconds: {0}", dateDiff.Seconds );
// > DateDiff.Seconds: 42986489
// elapsed
Console.WriteLine( "DateDiff.ElapsedYears: {0}", dateDiff.ElapsedYears );
// > DateDiff.ElapsedYears: 1
Console.WriteLine( "DateDiff.ElapsedMonths: {0}", dateDiff.ElapsedMonths );
// > DateDiff.ElapsedMonths: 4
Console.WriteLine( "DateDiff.ElapsedDays: {0}", dateDiff.ElapsedDays );
// > DateDiff.ElapsedDays: 12
Console.WriteLine( "DateDiff.ElapsedHours: {0}", dateDiff.ElapsedHours );
// > DateDiff.ElapsedHours: 12
Console.WriteLine( "DateDiff.ElapsedMinutes: {0}", dateDiff.ElapsedMinutes );
// > DateDiff.ElapsedMinutes: 41
Console.WriteLine( "DateDiff.ElapsedSeconds: {0}", dateDiff.ElapsedSeconds );
// > DateDiff.ElapsedSeconds: 29
} // DateDiffSample
I also needed this but, in my case, without the weeks part (so only years, months and days). Given that, here's what I made:
DateTime startDate = DateTime.ParseExact (start, "dd/MM/yyyy",CultureInfo.InvariantCulture);
DateTime endDate = DateTime.ParseExact (end, "dd/MM/yyyy",CultureInfo.InvariantCulture);
int days=0;
int months = 0;
int years = 0;
//calculate days
if (endDate.Day >= startDate.Day) {
days = endDate.Day - startDate.Day;
} else {
var tempDate = endDate.AddMonths (-1);
int daysInMonth = DateTime.DaysInMonth (tempDate.Year, tempDate.Month);
days = daysInMonth - (startDate.Day - endDate.Day);
months--;
}
//calculate months
if (endDate.Month >= startDate.Month) {
months+=endDate.Month - startDate.Month;
} else {
months+= 12 - (startDate.Month - endDate.Month);
years--;
}
//calculate years
years+=endDate.Year - startDate.Year;
Debug.WriteLine (string.Format("{0} years, {1} months, {2} days",years,months,days));
If you want to show this more dynamically, you can also use this code:
//build the string
var result = "";
if (years!=0){
result = years == 1 ? years + " year" : years + " years";
}
if (months != 0) {
if (result != "") {
result += ", ";
}
result += months == 1 ? months + " month" : months + " months";
}
if (days != 0) {
if (result != "") {
result += ", ";
}
result += days == 1 ? days + " day" : days + " days";
}
Debug.WriteLine (result);
This should do it. The key is to reduce days by 1 if there are odd number of leap days in between the given 2 dates.
/// <summary>
/// //Assume DateTime dt1 < DateTime dt2, print out difference between dt1 to dt2 in years, months, weeks and days
/// </summary>
/// <param name="dt1"></param>
/// <param name="dt2"></param>
static void DateDiff(DateTime dt1, DateTime dt2)
{
DateTime zeroTime = new DateTime(1, 1, 1);
int leapDaysInBetween = CountLeapDays(dt1, dt2);
TimeSpan span = dt2 - dt1;
int years = (zeroTime + span).Year - 1;
int months = (zeroTime + span).Month - 1;
int days = (zeroTime + span).Day - (leapDaysInBetween % 2 == 1 ? 1 : 0);
int weeks = days / 7;
int remainingdays = days % 7;
Console.WriteLine(String.Format("\nThe difference between date {0} and date {1} is: \n\t {2} year(s), {3} month(s), and {4} day(s).", dt1, dt2, years, months, days));
Console.WriteLine(String.Format("\nThe difference between date {0} and date {1} is: \n\t {2} year(s), {3} month(s), {4} week(s) and {5} day(s).", dt1, dt2, years, months, weeks, remainingdays));
}
private static int CountLeapDays(DateTime dt1, DateTime dt2)
{
int leapDaysInBetween = 0;
int year1 = dt1.Year, year2 = dt2.Year;
DateTime dateValue;
for (int i = year1; i <= year2; i++)
{
if (DateTime.TryParse("02/29/" + i.ToString(), out dateValue))
{
if (dateValue >= dt1 && dateValue <= dt2)
leapDaysInBetween++;
}
}
return leapDaysInBetween;
}
Had run these tests:
static void Main(string[] args)
{
DateDiff(new DateTime(2010, 1, 1), new DateTime(2012, 2, 9));
DateDiff(new DateTime(2010, 1, 1), new DateTime(2012, 4, 9));
DateDiff(new DateTime(2010, 1, 1), new DateTime(2020, 2, 9));
DateDiff(new DateTime(2010, 1, 1), new DateTime(2020, 4, 9));
DateDiff(new DateTime(2020, 2, 29), new DateTime(2021, 2, 28));
DateDiff(new DateTime(2019, 2, 28), new DateTime(2021, 2, 28));
}
These are the printouts:
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2012 12:00:00 AM is: 2 year(s), 3 month(s), and 9 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2012 12:00:00 AM is: 2 year(s), 3 month(s), 1 week(s) and 2 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 2/9/2020 12:00:00 AM is: 10 year(s), 1 month(s), and 9 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 2/9/2020 12:00:00 AM is: 10 year(s), 1 month(s), 1 week(s) and 2 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2020 12:00:00 AM is: 10 year(s), 3 month(s), and 9 day(s).
The difference between date 1/1/2010 12:00:00 AM and date 4/9/2020 12:00:00 AM is: 10 year(s), 3 month(s), 1 week(s) and 2 day(s).
The difference between date 2/29/2020 12:00:00 AM and date 2/28/2021 12:00:00 AM is: 1 year(s), 0 month(s), and 0 day(s).
The difference between date 2/29/2020 12:00:00 AM and date 2/28/2021 12:00:00 AM is: 1 year(s), 0 month(s), 0 week(s) and 0 day(s).
The difference between date 2/28/2019 12:00:00 AM and date 2/28/2021 12:00:00 AM is: 2 year(s), 0 month(s), and 1 day(s).
The difference between date 2/28/2019 12:00:00 AM and date 2/28/2021 12:00:00 AM is: 2 year(s), 0 month(s), 0 week(s) and 1 day(s).
I think TimeSpan is what you are looking for, but it does not do years or months because those vary in length.
The below example is from the above link;
// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString());
// Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays);
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours);
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks);
// the example displays the following output:
// 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
// Value of Days Component: 229
// Total Number of Days: 229.229340277778
// Value of Hours Component: 5
// Total Number of Hours: 5501.50416666667
// Value of Minutes Component: 30
// Total Number of Minutes: 330090.25
// Value of Seconds Component: 15
// Total Number of Seconds: 19,805,415
// Value of Milliseconds Component: 0
// Total Number of Milliseconds: 19,805,415,000
// Ticks: 198,054,150,000,000
public partial class Age1 : System.Web.UI.Page
{
private int Years;
private int Months;
private int Days;
DateTime Cday;
DateTime Bday;
protected void Page_Load(object sender, EventArgs e)
{
txtCurrentDate.Enabled = false;
txtCurrentDate.Text = DateTime.Now.ToString("g");
Cday = Convert.ToDateTime(txtCurrentDate.Text);
}
protected void Button1_Click(object sender, EventArgs e)
{
Bday = Convert.ToDateTime(txtBirthdate.Text);
AgeCaluclation(Bday, Cday);
txtBirthdate.Text = "";
txtCurrentDate.Text = "";
lblAge.Text = this.Years+" Years "+this.Months+" Months " +this.Days+ "Days";
}
private Age1 AgeCaluclation(DateTime Bday, DateTime Cday)
{
if ((Cday.Year - Bday.Year) > 0 ||
(((Cday.Year - Bday.Year) == 0) &&
((Bday.Month < Cday.Month) ||
((Bday.Month == Cday.Month) &&
(Bday.Day <= Cday.Day)))))
{
int DaysInBdayMonth = DateTime.DaysInMonth(Bday.Year, Bday.Month);
int DaysRemain = Cday.Day + (DaysInBdayMonth - Bday.Day);
if(Cday.Month > Bday.Month)
{
this.Years = Cday.Year - Bday.Year;
this.Months = Cday.Month - (Bday.Month + 1) + Math.Abs(DaysRemain / DaysInBdayMonth);
this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
}
else if (Cday.Month == Bday.Month)
{
if (Cday.Day >= Bday.Day)
{
this.Years = Cday.Year - Bday.Year;
this.Months = 0;
this.Days = Cday.Day - Bday.Day;
}
else
{
this.Years = (Cday.Year - 1) - Bday.Year;
this.Months = 11;
this.Days = DateTime.DaysInMonth(Bday.Year, Bday.Month) - (Bday.Day - Cday.Day);
}
}
else
{
this.Years = (Cday.Year - 1) - Bday.Year;
this.Months = Cday.Month + (11 - Bday.Month) + Math.Abs(DaysRemain / DaysInBdayMonth);
this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
}
}
else
{
throw new ArgumentException("Birthday date must be earlier than current date");
}
return this;
}
}
I had created this one for returning difference in years,month and days between 2 dates.
public static Dictionary<string, int> TimeSpanToDateParts(DateTime fromDate,DateTime toDate)
{
int years;
int months;
int days;
Dictionary<string, int> dateParts = new Dictionary<string, int>();
if (toDate < fromDate)
{
return TimeSpanToDateParts(toDate,fromDate);
}
var span = toDate - fromDate;
months = 12 * (toDate.Year - fromDate.Year) + (toDate.Month - fromDate.Month);
if (toDate.CompareTo(fromDate.AddMonths(months).AddMilliseconds(-500)) <= 0)
{
--months;
}
years = months / 12;
months -= years * 12;
if (months == 0 && years == 0)
{
days = span.Days;
}
else
{
days = toDate.Day;
if (fromDate.Day > toDate.Day)
days = days + (DateTime.DaysInMonth(toDate.Year, toDate.Month - 1) - fromDate.Day);
else
days = days - fromDate.Day;
}
dateParts.Add("Years", years);
dateParts.Add("Months", months);
dateParts.Add("Days", days);
return dateParts;
}
Investigate the C# TimeSpan structure. It doesn't do months or years, but it does do days. That makes things easier.
The logic isn't overly complicated if you first compute the total months and the total days. Then it's easy to do a bit of integer maths with divide and mod.
DateTime start_date = new DateTime(2010, 1, 1);
DateTime end_date = new DateTime(2011, 3, 2);
int diff_years = end_date.Year - start_date.Year;
int diff_months = end_date.Month - start_date.Month;
int total_months = 12 * diff_years + diff_months;
DateTime near_end_date = start_date.AddMonths(total_months);
int total_days = (int)end_date.Subtract(near_end_date).TotalDays;
int years = total_months / 12;
int months = total_months % 12;
int weeks = total_days / 7;
int days = total_days % 7;
Console.WriteLine($"Years: {years} Months: {months} Weeks: {weeks} Days: {days}");
That gives Years: 1 Months: 2 Weeks: 0 Days: 1
as expected.
Heres a converter I just built. I didn't try all of the solutions above but all the ones I did try were still slightly off in one way or another. This takes into account Years, Leap Years, Months, Weeks, Days etc and then grabs the two most useful values last YM or MW or WD or DH. It's simple but I needed it for my project maybe could be of help to someone down the road. And then of course if this is not good for one reason or another I'm sure you all will let me know.
DateTime beforeDate = topic.lastActive.ToLocalTime();
DateTime futureDate = DateTime.Now.ToLocalTime();
int minutes = 0;
int hours = 0;
int days = 0;
int weeks = 0;
int months = 0;
int years = 0;
Dictionary<int, int> dictMonths = new Dictionary<int, int> { };
dictMonths.Add(1, 31);
if (DateTime.IsLeapYear(futureDate.Year))
dictMonths.Add(2, 29);
else
dictMonths.Add(2, 28);
dictMonths.Add(3, 31);
dictMonths.Add(4, 30);
dictMonths.Add(5, 31);
dictMonths.Add(6, 30);
dictMonths.Add(7, 31);
dictMonths.Add(8, 31);
dictMonths.Add(9, 30);
dictMonths.Add(10, 31);
dictMonths.Add(11, 30);
dictMonths.Add(12, 31);
//Time difference between dates
TimeSpan span = futureDate - beforeDate;
hours = span.Hours;
minutes = span.Minutes;
//Days total
days = span.Days;
//Find how many years
DateTime zeroTime = new DateTime(1, 1, 1);
// Because we start at year 1 for the Gregorian
// calendar, we must subtract a year here.
years = (zeroTime + span).Year - 1;
//find difference of days of years already found
int startYear = futureDate.Year - years;
for (int i = 0; i < years; i++)
{
if (DateTime.IsLeapYear(startYear))
days -= 366;
else
days -= 365;
startYear++;
}
//Find months by multiplying months in year by difference of datetime years then add difference of current year months
months = 12 * (futureDate.Year - beforeDate.Year) + (futureDate.Month - beforeDate.Month);
//month may need to be decremented because the above calculates the ceiling of the months, not the floor.
//to do so we increase before by the same number of months and compare.
//(500ms fudge factor because datetimes are not precise enough to compare exactly)
if (futureDate.CompareTo(beforeDate.AddMonths(months).AddMilliseconds(-500)) <= 0)
{
--months;
}
//subtract months from how many years we have already accumulated
months -= (12 * years);
//find how many days by compared to our month dictionary
int startMonth = beforeDate.Month;
for (int i = 0; i < months; i++)
{
//check if faulty leap year
if (startMonth == 2 && (months - 1 > 10))
days -= 28;
else
days -= dictMonths[startMonth];
startMonth++;
if (startMonth > 12)
{
startMonth = 1;
}
}
//Find if any weeks are within our now total days
weeks = days / 7;
if (weeks > 0)
{
//remainder is days left
days = days % 7;
}
Console.WriteLine(years + " " + months + " " + weeks + " " + days + " " + span.Hours + " " + span.Minutes);
if (years > 0)
{
if (years > 1 && months > 1)
return $"Latest Reply: {years} years, {months} months ago.";
else if (years > 1 && months == 0)
return $"Latest Reply: {years} years ago.";
else if (years == 1 && months == 0)
return $"Latest Reply: {years} year ago.";
else if (years > 1)
return $"Latest Reply: {years} years, {months} month ago.";
else if (months > 1)
return $"Latest Reply: {years} year, {months} months ago.";
else
return $"Latest Reply: {years} year, {months} month ago.";
}
else if (months > 0)
{
if (months > 1 && weeks > 1)
return $"Latest Reply: {months} months, {weeks} weeks ago.";
else if (months > 1 && weeks == 0)
return $"Latest Reply: {months} months ago.";
else if (months == 1 && weeks == 0)
return $"Latest Reply: {months} month ago.";
else if (months > 1)
return $"Latest Reply: {months} months, {weeks} week ago.";
else if (weeks > 1)
return $"Latest Reply: {months} month, {weeks} weeks ago.";
else
return $"Latest Reply: {months} month, {weeks} week ago.";
}
else if (weeks > 0)
{
if (weeks > 1 && days > 1)
return $"Latest Reply: {weeks} weeks, {days} days ago.";
else if (weeks > 1 && days == 0)
return $"Latest Reply: {weeks} weeks ago.";
else if (weeks == 1 && days == 0)
return $"Latest Reply: {weeks} week ago.";
else if (weeks > 1)
return $"Latest Reply: {weeks} weeks, {days} day ago.";
else if (days > 1)
return $"Latest Reply: {weeks} week, {days} days ago.";
else
return $"Latest Reply: {weeks} week, {days} day ago.";
}
else if (days > 0)
{
if (days > 1 && hours > 1)
return $"Latest Reply: {days} days, {hours} hours ago.";
else if (days > 1 && hours == 0)
return $"Latest Reply: {days} days ago.";
else if (days == 1 && hours == 0)
return $"Latest Reply: {days} day ago.";
else if (days > 1)
return $"Latest Reply: {days} days, {hours} hour ago.";
else if (hours > 1)
return $"Latest Reply: {days} day, {hours} hours ago.";
else
return $"Latest Reply: {days} day, {hours} hour ago.";
}
else if (hours > 0)
{
if (hours > 1 && minutes > 1)
return $"Latest Reply: {hours} hours, {minutes} minutes ago.";
else if (hours > 1 && minutes == 0)
return $"Latest Reply: {hours} hours ago.";
else if (hours == 1 && minutes == 0)
return $"Latest Reply: {hours} hour ago.";
else if (hours > 1)
return $"Latest Reply: {hours} hours, {minutes} minute ago.";
else if (minutes > 1)
return $"Latest Reply: {hours} hour, {minutes} minutes ago.";
else
return $"Latest Reply: {hours} hour, {minutes} minute ago.";
}
else if (minutes > 0)
{
if (minutes > 1)
return $"Latest Reply: {minutes} minutes ago.";
else
return $"Latest Reply: {minutes} minute ago.";
}
else
{
return $"Latest Reply: Just now.";
}
It's quite an old question, but still time to have a go.
Important to consider that months have differing number of days.
public static (int, int, int) GetYearsMonthsDaysDifference(DateTime date1, DateTime date2)
{
DateTime startDate = date1;
DateTime endDate = date2;
if (date1 > date2)
{
startDate = date2;
endDate = date1;
}
var months = (endDate.Month + (12 - startDate.Month)) - (endDate.Day < startDate.Day ? 1 : 0);
var years = (endDate.Year - startDate.Year - (months < 12 ? 1 : 0));
months -= (months >= 12 ? 12 : 0);
var days = endDate.DayOfYear - startDate.AddYears(years).AddMonths(months).DayOfYear;
return (years, months, days);
}
精彩评论