thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
This might not be the best solution, but if you don't find anything better:
years.Text = DateDiff(DateInterval.Year, Start.Value, _End.Value)
months.Text = DateDiff(DateInterval.Month, _
DateAdd(DateInterval.Year, CInt(years.Text), Start.Value), _End.Value)
days.Text = DateDiff(DateInterval.Day, _
DateAdd(DateInterval.Month, CInt(months.Text), _
DateAdd(DateInterval.Year, CInt(years.Text), Start.Value)), _End.Value)
hours.Text = DateDiff(DateInterval.Hour, _
DateAdd(DateInterval.Day, CInt(days.Text), _
DateAdd(DateInterval.Month, CInt(months.Text), _
DateAdd(DateInterval.Year, CInt(years.Text), Start.Value) _
)), _End.Value)
Sometimes Excel logic can do wonders.
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
adam_k nailed it.
I wasn't paying attention.
Here is my WinForms version:
Public Class Form1
Private Sub bnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnCalculate.Click
Dim dateStart As Date = DateTimePickerStart.Value
Dim dateEnd As Date = DateTimePickerEnd.Value
Dim tsDiff As TimeSpan = dateEnd - dateStart
Dim intYears As Integer = Math.Floor((tsDiff.Days / 365))
Dim intCalcYears As Integer = (intYears * 365)
Dim intMonths As Integer = Math.Floor((tsDiff.Days - intCalcYears) / 30)
Dim intDays As Integer = tsDiff.Days - intCalcYears - (intMonths * 30)
MessageBox.Show(String.Format("Years={0} Months={1} Days={2}",
intYears, intMonths, intDays))
End Sub
End Class
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
Why would you go in fixed days per year/month, when the above solution will return actual years/months? ie. Jan 1st to March 1st how many months is it? To make it even worse Jan 1st 2011 - March 1st 2011 vs Jan 1st 2012 - March 1st 2012 should return the same result in months or not?
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7