We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,688 Members — Technology Publication meets Social Media

Problem with dates calculation

I've been given two dates that takes their values from two respective dtpickers.
I've got to calculate the exact amount of days, months and years between them.
So I tried this:
Dim dteStartDate As Date
Dim dteEndDate As Date
dteStartDate = dtpStart.Text
dteEndDate = dtpEnd.Text
dteResultDays = DateDiff(DateInterval.Day, dteStartDate, dteEndDate)
dteResultMonths = DateDiff(DateInterval.Month, dteStartDate, dteEndDate)
dteResultYears = DateDiff(DateInterval.Year, dteStartDate, dteEndDate)

The problem is that this method gives me ALL of the amount of respective days, months and years passed, but I need to split them in the correct way. Ex, between 28/2/98 and 13/1/2007 the result should be 15 days, 10 months and 8 years. And above all, another bug is that the function indicates one month more (or one year more) with only one day (or month) passed between the two dates! They told me I shoul use TimeSpan,but i don't Know how...

Any brighter ideas? I'd be very grateful...and it will grant you an excellent karma :-)

5
Contributors
6
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
7
Views
MassimilianoRiz
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Skill Endorsements: 0

There are a few TimeSpan examples in that post.

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Skill Endorsements: 7
bhagawatshinde
Posting Whiz
338 posts since Sep 2010
Reputation Points: 21
Skill Endorsements: 0

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, _
days.Text = DateDiff(DateInterval.Day, _
hours.Text = DateDiff(DateInterval.Hour, _
)), _End.Value)``````

Sometimes Excel logic can do wonders.

Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Skill Endorsements: 11

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
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
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?

Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Skill Endorsements: 11

You're right.

thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447