locked
Finding difference between two dates RRS feed

  • Question

  • I have 2 datetimepickers, and need to find the difference between them. I realized that this was much more complicated than I first thought (leap years, Jan 1 is only 1 day away from Dec 31, etc). How do I calculate the difference between the two dates taken from datetimepickers? Here's the code I came up with, but it has issues with days and (I'm pretty sure) months:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim dte1 As Date = DateTimePicker1.Value
            Dim dte2 As Date = DateTimePicker2.Value
    
            Dim day1 As Integer = DateTimePicker1.Value.DayOfYear
            Dim day2 As Integer = DateTimePicker2.Value.DayOfYear
    
            Dim month1 As Integer = DateTimePicker1.Value.Month
            Dim month2 As Integer = DateTimePicker2.Value.Month
    
            Dim year1 As Integer = DateTimePicker1.Value.Year
            Dim year2 As Integer = DateTimePicker2.Value.Year
    
            Dim days As Integer = day2 - day1
            Dim months As Integer = month2 - month1
            Dim years As Integer = year2 - year1
    
            If days < 0 Then
                days *= -1
            End If
    
            If months < 0 Then
                months *= -1
            End If
    
            If years < 0 Then
                years *= -1
            End If
    
            MessageBox.Show(years & " years, " & months & " months, " & days & " days")
        End Sub

    Friday, April 12, 2013 7:23 AM

Answers

All replies

  • The Timespan object allows you to calculate the difference between two datetimes by subtraction.

    http://msdn.microsoft.com/en-AU/library/system.timespan.aspx

    (example 3)

    • Edited by Acamar Friday, April 12, 2013 7:44 AM sp
    • Proposed as answer by Cor Ligthert Friday, April 12, 2013 8:28 AM
    • Marked as answer by Rahul Y Friday, April 12, 2013 2:59 PM
    Friday, April 12, 2013 7:44 AM
  • Hi Acamar,

    Thanks, that link was very useful. Is there a way to differentiate between days, months and years? I mean like 1 year, 3 months and 4 days? Here is the code I now use:

    Dim dte1 As Date = DateTimePicker1.Value Dim dte2 As Date = DateTimePicker2.Value If dte1 > dte2 Then Dim dte3 As Date = dte2 dte2 = dte1 dte1 = dte3 End If Dim interval As TimeSpan = dte2 - dte1 MessageBox.Show("The interval is " & interval.Days.ToString() & " days")

    Friday, April 12, 2013 2:11 PM
  • Hi Acamar,

    Thanks, that link was very useful. Is there a way to differentiate between days, months and years? I mean like 1 year, 3 months and 4 days? Here is the code I now use:

    Dim dte1 As Date = DateTimePicker1.Value Dim dte2 As Date = DateTimePicker2.Value If dte1 > dte2 Then Dim dte3 As Date = dte2 dte2 = dte1 dte1 = dte3 End If Dim interval As TimeSpan = dte2 - dte1 MessageBox.Show("The interval is " & interval.Days.ToString() & " days")


    Depends on how accurate you want it to be?

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Friday, April 12, 2013 2:25 PM
  • Hi

    This brings in the dreaded time definition problem. What is a year? What is a month? Does an average year contain 365.25 days? How many days in a month? I would certainly steer away from using Months, and Years are a pain also.


    Regards Les, Livingston, Scotland

    • Proposed as answer by Cor Ligthert Friday, April 12, 2013 2:46 PM
    Friday, April 12, 2013 2:34 PM
  • Hi

    This brings in the dreaded time definition problem. What is a year? What is a month? Does an average year contain 365.25 days? How many days in a month? I would certainly steer away from using Months, and Years are a pain also.


    Regards Les, Livingston, Scotland

    I couldn't agree more.

    See this discussion

    http://www.vbforums.com/showthread.php?655833-Calculating-exact-time-in-Years-MOnths-Days&highlight=age+year+months+days


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein


    • Edited by dbasnett Friday, April 12, 2013 3:29 PM
    Friday, April 12, 2013 2:43 PM
  • Hi

    This brings in the dreaded time definition problem. What is a year? What is a month? Does an average year contain 365.25 days? How many days in a month? I would certainly steer away from using Months, and Years are a pain also.


    Regards Les, Livingston, Scotland

    See this discussion

    http://www.vbforums.com/showthread.php?655833-Calculating-exact-time-in-Years-MOnths-Days&highlight=age+year+months+days


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    That does not show what Les and I share as problem that persons don't define what they mean with duration calculations. To calculate the exact time is very simple in current VB.

    However, what where the milleniums between 1999-12-31 23:99 and 2000-01-01 00.00

    One and therefore thousand years, 12000 months, and so on?


    Success
    Cor


    Friday, April 12, 2013 2:51 PM
  • "That does not show what Les and I share as problem..."  What 'That' are you referring to?

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Friday, April 12, 2013 2:57 PM
  • "That does not show what Les and I share as problem..."  What 'That' are you referring to?

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    The link in your message and quoted by me. That shows the difference between two dates exact even in microseconds.

    Success
    Cor

    Friday, April 12, 2013 3:06 PM
  • The timespan structure gives you a time period that can be expressed in any time unit from ticks to days, either as whole units or whole and fractional units.  You can convert those values to other time intervals, but a time unit such as month or year creates problems due to the fact that these units vary in length. 

    One technique I have used is to count days to the end of the first month, whole months to the end of the first year, whole years to the year of the end date, whole months in the end year and days in the end month.   I can then add months together to see if I have to add another year and reduce the months by 12, but I then might end up with a lot of days, such as 42.  In that case, how do I express the last days of the first month and the first days of the last month as an additional month plus a few days? A decision must be made about the size of a month.

    Friday, April 12, 2013 9:53 PM