We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# Complete elapsed time in Years Months Days Hours and Minutes

on
Medium Priority
219 Views
Hi,

How can I format an elapsed time to include the number of years, months, days, hours and minutes.

For example:

DT1 = New DateTime(2004, 10, 27, 8, 20, 0)
DT2 = New DateTime(2006, 11, 28, 11, 47, 0)

I want to get:

«Elapsed time is:
2 years
1 month
1 day
3 hours
27 minutes»

Thanks !

scml
Comment
Watch Question

## View Solution Only

Commented:
dim ts as timespan = DT2.Substract(DT1)

then ts will contain all the info you need. for example ts.TotalDays, ts.TotalHours, etc.

Commented:
In my example:

DT1 = New DateTime(2004, 10, 27, 8, 20, 0)
DT2 = New DateTime(2006, 11, 28, 11, 47, 0)

this gives me a total in days of 762.14375 but it's not what I need. I need to have 1 day i.e. 2 years, 1 month, 1 day, x hours, x minutes.

scml

Commented:
I'm not sure it's quite that simple.  The maximum unit that TimeSpan works in is Days.  When we get up to months and years, they can vary in length: a year (with leap years) from 365 to 366 and a month from 28 to 31.  So, if the TimeSpan.Duration shows 32 days, do you want your result to be

1 Month
4 days

or

1 Month
3 days

or

1 Month
2 days

or

1 Month
1 Day

Or if the difference is 367 days, do you want your result to be

1 Year
0 Months
2 Days

or

1 Year
0 Months
1 Day

It is (will be) possible to produce the answer that you want but (a) you will have to decide which of the above options is to be applied and (b) it will be necessary then to analyse the dates AS DATES - i.e. looking at the year, the month, the day in the month, the hour, the minute in the hour, even the second in the minute - in DT1 and DT2 rather than just doing a TimeSpan subtraction.

Roger

Commented:
I would need:

1 Year
0 Months (or 1 to 12... depending on calculation)
1 Day  (or 1 or 2 or... 30 or 31... depending on calculation)

Thanks !

scml

Commented:
Here's a module that should do most of what you want

Module modDates

Public Structure myDate
Dim Year As Integer
Dim Month As Integer
Dim Day As Integer
Dim Hour As Integer
Dim Minute As Integer
Dim Second As Integer
End Structure

Private thismonth As Integer

Private Function DaysInMonth(ByVal month As Integer) As Integer
Select Case month
Case 1, 3, 5, 7, 8, 10, 12
Return 31
Case 2
Return 28 'NOTE takes no account of Leap Years
Case 4, 6, 9, 11
Return 30
Case Else
MsgBox("Not a valid month")
Return 0
End Select
End Function

Public Function ElapsedTime(ByVal StartDate As myDate, ByVal EndDate As myDate) As myDate
thismonth = StartDate.Month 'needed for CarryDay
Dim result As New myDate
result.Year = EndDate.Year - StartDate.Year
result.Month = EndDate.Month - StartDate.Month
If result.Month < 0 Then
CarryMonth(result)
End If
result.Day += EndDate.Day - StartDate.Day
If result.Day < 0 Then
CarryDay(result)
End If
result.Hour += EndDate.Hour - StartDate.Hour
If result.Hour < 0 Then
CarryHour(result)
End If
result.Minute += EndDate.Minute - StartDate.Minute
If result.Minute < 0 Then
CarryMinute(result)
End If
result.Second += EndDate.Second - StartDate.Second
If result.Second < 0 Then
CarrySecond(result)
End If
If result.Year < 0 Then
MsgBox("End date is before start date")
Return New myDate
End If
Return result
End Function

Private Sub CarrySecond(ByRef thisDate As myDate)
thisDate.Second += 60
thisDate.Minute -= 1
If thisDate.Minute < 0 Then CarryMinute(thisDate)
End Sub

Private Sub CarryMinute(ByRef thisDate As myDate)
thisDate.Minute += 60
thisDate.Hour -= 1
If thisDate.Hour < 0 Then CarryHour(thisDate)
End Sub

Private Sub CarryHour(ByRef thisDate As myDate)
thisDate.Hour += 24
thisDate.Day -= 1
If thisDate.Day < 0 Then CarryDay(thisDate)
End Sub

Private Sub CarryDay(ByRef thisDate As myDate)
thisDate.Day += DaysInMonth(thismonth)
thisDate.Month -= 1
If thisDate.Month < 0 Then CarryMonth(thisDate)
End Sub

Private Sub CarryMonth(ByRef thisDate As myDate)
thisDate.Month += 12
thisDate.Year -= 1
If thisDate.Year < 0 Then
MsgBox("End date is before start date")
End If
End Sub

End Module

I say "most of" as - see comment above - it does not deal with Leap Years.  Otherwise, so far as my tests have taken me, it seems OK.

If you put each of your two dates into an instance of the myDate structure and pass those as the arguments of the ElapsedTime() function it will return a myDate structure with the difference in.

I have to say, though, that I don't really think this is "elapsed TIME".  The point here is that two identical structures can - because of the different numbers of days per month - in fact be representing different periods of time: and two different structures can be representing an identical period of time.  But it is, as I understand it, what you asked for.

Roger

Commented:
I will give another example:

Let's say my baby was born
Jan 11 2006 01:00 AM.

Today's date is
Nov 7 2006 10:30 AM.

So my baby's age is:
0 years, 9 months, 26 days, 9 hours, 30 minutes

Birth:
Jan 11 2005 01:00 AM.

Today's date is
Nov 7 2006 10:30 AM.

So my baby's age is:
1 years, 9 months, 26 days, 9 hours, 30 minutes

scml

Commented:
Precision:
I need to take leap years into account.

So let's say we are in 2008, my previous example's dates would become:

Birth:
Jan 11 2007 01:00 AM.

Today's date is
Nov 7 2008 10:30 AM.

Commented:
I'm afraid your last comment just illustrates my point.  Although

Birth:
Jan 11 2006 01:00 AM.

Today's date is
Nov 7 2007 10:30 AM.

on the one hand and

Birth:
Jan 11 2007 01:00 AM.

Today's date is
Nov 7 2008 10:30 AM.

would be different time spans they would, expressed in the way you want them, be identical.  Just because February has 28 days in some years and 29 in others it doesn't stop it being a "Month".

On the basis of your original illustration

Birth:
Jan 11 2007 01:00 AM.

Today's date is
Feb 11 2007 01:00 AM.

would give an answer of 1 Month, and so would

Birth:
Feb 11 2007 01:00 AM.

Today's date is
Mar 11 2007 01:00 AM.

That the first is a month containing 31 days and the second a month containing 28 days is, as I understood the original spec, irrelevant.  If you want Months in your answers, but you don't want that effect, then you will have to define what you mean by month.

Roger

Not the solution you were looking for? Getting a personalized solution is easy.

##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile