# Thread: Date difference function (time elapsed)

1. Registered User
Join Date
Oct 2004
Posts
10

## Unanswered: Date difference function (time elapsed)

Hi, I am wondering if anyone knows where to find a function that returns the time elapsed between two dates.
I have todays date and a date in the past in the form mm/dd/yyyy and need to have the number of years, months, and days that have elapsed. For example, since mm/dd/yyyy - "5 years 2 months and 12 days have passed"
The Datediff function does not do this completely.
Any input would incredibly appreciated.
Thanks!!!
Steve
Last edited by svgiles; 08-19-06 at 16:22.

2. Registered User
Join Date
Mar 2004
Posts
5

## Computed time elapsed

Well, Steve, you DID say ANY input!!

While likely not the most accurate mathematically, here's where I am so far. I think works using DayOfYear as opposed to Day but dMonth creates the inaccuracy. Might be able to get a better algorithm once I think more about it:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim EndDate, StartDate As Date
Dim dYears, dMonth, dDays As String

StartDate = Convert.ToDateTime(TextBox1.Text)
EndDate = Convert.ToDateTime(TextBox2.Text)
dYears = DateDiff(DateInterval.Year, StartDate, EndDate)
dMonth = DateDiff(DateInterval.Month, StartDate, EndDate) - (dYears * 12)
dDays = (DateDiff(DateInterval.DayOfYear, StartDate, EndDate) - (dYears * 365) - (dMonth * 30))

Label1.Text = dYears & "Year(s), " & dMonth & " Month(s), " & dDays & " Day(s) have passed."
End Sub

Best to you,
MKS
Last edited by kscheidel; 09-11-06 at 22:55.

3. Registered User
Join Date
Oct 2004
Posts
10

## Thank you!!

Thank you, MKS!!
I do very much appreciate your input!!
-Steve

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•