Solved

Complete elapsed time in Years Months Days Hours and Minutes

Posted on 2006-11-06
8
199 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:scml
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17885601
dim ts as timespan = DT2.Substract(DT1)

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

Author Comment

by:scml
ID: 17886037
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

0
 
LVL 34

Expert Comment

by:Sancler
ID: 17886101
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
0
 

Author Comment

by:scml
ID: 17886290
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 34

Expert Comment

by:Sancler
ID: 17890098
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
0
 

Author Comment

by:scml
ID: 17890160
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
0
 

Author Comment

by:scml
ID: 17890310
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.

0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17890984
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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now