none
How Many Years, Months, Weeks that has past between two dates RRS feed

  • Question

  • Hi Good People

    Can You help me I can Write Code for Days, Hours, Minutes, Seconds Using the word Difference As in the Code Below.

    TextBox4.Text = FormatNumber(Difference.TotalDays, 0)
            TextBox5.Text = FormatNumber(Difference.TotalHours, 0)
            TextBox6.Text = FormatNumber(Difference.TotalMinutes, 0)
            TextBox7.Text = FormatNumber(Difference.TotalSeconds, 0)

    But I cannot use the same code for Years, Months, Weeks. Is there another type of coding to get the Total Years, Months, Weeks That have past between two Date I select from 2 DateTimePickers. The code I have at the moment is below. Where I am having problems is the code for TextBox1 to Textbox3, Can Someone please point me in the right Direction.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Timer1.Start() Dim Date1 As Date Dim Date2 As Date Dim Difference As TimeSpan Dim DayOne As FirstDayOfWeek = DateTimePicker1.Value Date1 = Convert.ToDateTime(DateTimePicker1.Value) Date2 = Convert.ToDateTime(DateTimePicker2.Value) Difference = Date2.Subtract(Date1)

    'TextBox1.Text = FormatNumber(Difference.TotalYears, 0) '0 is for Decimal Places 'TextBox2.Text = FormatNumber(Difference.TotalMonths, 0) '0 is for Decimal Places 'TextBox3.Text = FormatNumber(Difference.Weeks, 0) '0 is for Decimal Places

    TextBox4.Text = FormatNumber(Difference.TotalDays, 0) TextBox5.Text = FormatNumber(Difference.TotalHours, 0) TextBox6.Text = FormatNumber(Difference.TotalMinutes, 0) TextBox7.Text = FormatNumber(Difference.TotalSeconds, 0) End Sub



    Gary Simpson

    Saturday, June 15, 2019 2:33 PM

Answers

  • Check this approach:

     

    Dim totalYears = 0

    Dim totalMonths = 0

    Dim totalWeeks = 0

     

    Dim d1 = Date1

    Dim d2 = Date2

    Dim n = 1

    If d1 > d2 Then

        n = -1

        d1 = Date2

        d2 = Date1

    End If

     

    While d1.AddYears(totalYears + 1) < d2

        totalYears += 1

    End While

     

    While d1.AddMonths(totalMonths + 1) < d2

        totalMonths += 1

    End While

     

    totalYears *= n

    totalMonths *= n

    totalWeeks = CInt(Difference.TotalDays) \ 7

     

    Then use FormatNumber and show results in textboxes.

    • Marked as answer by Gary Simpson Saturday, June 15, 2019 5:22 PM
    Saturday, June 15, 2019 4:37 PM

All replies



  • Gary Simpson

    Saturday, June 15, 2019 3:07 PM
  • Check this approach:

     

    Dim totalYears = 0

    Dim totalMonths = 0

    Dim totalWeeks = 0

     

    Dim d1 = Date1

    Dim d2 = Date2

    Dim n = 1

    If d1 > d2 Then

        n = -1

        d1 = Date2

        d2 = Date1

    End If

     

    While d1.AddYears(totalYears + 1) < d2

        totalYears += 1

    End While

     

    While d1.AddMonths(totalMonths + 1) < d2

        totalMonths += 1

    End While

     

    totalYears *= n

    totalMonths *= n

    totalWeeks = CInt(Difference.TotalDays) \ 7

     

    Then use FormatNumber and show results in textboxes.

    • Marked as answer by Gary Simpson Saturday, June 15, 2019 5:22 PM
    Saturday, June 15, 2019 4:37 PM
  • Viorel

    Thank you very much For Putting me right and pointing me in the right Direction

    Your code works well. I did not know if \ 7 was the way to go but hey I got the results I wanted 

    Thanks again 

    Kind regards 

    Gary


    Gary Simpson

    Saturday, June 15, 2019 5:22 PM
  • In Case anyone is interested, On My form I have 

    DateTimePicker1

    DateTimePicker2

    10 Labels

    Label1 Saying( How Much Time has Passed Between Two Dates: )

    Label2 Saying: ( Start Date  )

    Label3 Saying: ( End Date  )

    Lable4 Saying: ( Number of Years  )

    Label5 Saying: ( Number of Months  )

    Label6 Saying: ( Number of Weeks  )

    Label7 Saying: ( Number of Days  )

    Label8 Saying: (  Number of Hours )

    Label9 Saying: ( Number of Minutes  )

    Label10 Saying: ( Number of Seconds  )

    7 Textboxes

    TextBox1

    TextBox2

    TextBox3

    TextBox4

    TextBox5

    TextBox6

    TextBox7

    4 Buttons

    Button1 Text Changed to (  Time Span Difference Between two Dates  )

    Button2 Text Changed to (  Reset Dates  )

    Button3 Text Changed to (  Close and Exit App   )

    Button4 Text Changed to ( Text X )Back colour Red, Fore color White

    1 Timer

    Timer1

    The Code I have with the help of Viorel

     'Start the Timer1 and converts to Different Time between selected Dates
            Timer1.Start()
            If DateTimePicker1.Text = DateTimePicker2.Text Then
                MsgBox(Environment.UserName & " Both Dates are the same and there will be No Values to show ", MsgBoxStyle.Exclamation, "No Values to show " & Environment.UserName)
                TextBox1.Clear()
                TextBox2.Clear()
                TextBox3.Clear()
                TextBox4.Clear()
                TextBox5.Clear()
                TextBox6.Clear()
                TextBox7.Clear()
            Else
                Dim Date1 As Date
                Dim Date2 As Date
                Dim Difference As TimeSpan
                Date1 = Convert.ToDateTime(DateTimePicker1.Value)
                Date2 = Convert.ToDateTime(DateTimePicker2.Value)
                Difference = Date2.Subtract(Date1)
                Dim totalYears = 0
                Dim totalMonths = 0
                Dim totalWeeks = 0
                Dim d1 = Date1
                Dim d2 = Date2
                Dim n = 1
                If d1 > d2 Then
                    n = -1
                    d1 = Date2
                    d2 = Date1
                End If
                While d1.AddYears(totalYears + 1) < d2
                    totalYears += 1
                End While
                While d1.AddMonths(totalMonths + 1) < d2
                    totalMonths += 1
                End While
                totalYears *= n
                totalMonths *= n
                totalWeeks = CInt(Difference.TotalDays) \ 7
                TextBox1.Text = totalYears
                TextBox2.Text = totalMonths
                TextBox3.Text = totalWeeks
                TextBox4.Text = FormatNumber(Difference.TotalDays, 0)
                TextBox5.Text = FormatNumber(Difference.TotalHours, 0)
                TextBox6.Text = FormatNumber(Difference.TotalMinutes, 0)
                TextBox7.Text = FormatNumber(Difference.TotalSeconds, 0)
            End If
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            'Reset Textboxes And DateTimePicker1 And DateTimePicker2
            DateTimePicker1.Value = Date.Now
            DateTimePicker2.Value = Date.Now
            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()
            TextBox6.Clear()
            TextBox7.Clear()
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            'Closes the Program
            MsgBox(Environment.UserName & " Close the Program ", MsgBoxStyle.Information, "Thank you for trying this Program " & Environment.UserName)
            Application.Exit()
        End Sub
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            'Closes the Program
            MsgBox(Environment.UserName & " Close the Program ", MsgBoxStyle.Information, "Thank you for trying this Program " & Environment.UserName)
            Application.Exit()
        End Sub

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MsgBox("If the Start date is higher than the End date the values will be negative values")
        End Sub
    End Class

    Thank you Regards Gary


    Gary Simpson


    Monday, June 17, 2019 7:06 PM
  • Getting the difference values for months and years can be difficult because they don't have the same amount days.

    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Monday, June 17, 2019 7:20 PM
  • dbasnett,

    Would you like me to remove the above code?

    Regards 

    Gary


    Gary Simpson

    Wednesday, June 19, 2019 12:08 AM