Getting the wrong number of days for days between dates

Discussion in 'Visual Basic ( VB )' started by Kenneth Reid, Dec 31, 2021.

  1. Kenneth Reid

    Kenneth Reid New Member

    Joined:
    Dec 22, 2021
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Occupation:
    Disabled
    I am trying to write a function to get the "Days between dates". The code I have (for a console application) is shown below. For some reason, it is not giving the correct number of days between dates. How do I get this to work correctly?
    Code:
    Dim BeginningDate As Date
    Dim EndingDate As Date
    
    Dim DaysBetweenDates = DateDiff(DateInterval.Day, EndingDate.Date, BeginningDate.Date)
    Dim msg = String.Format(" days between dates.", DaysBetweenDates)
    
    Console.WriteLine("                          Days Between Dates Subroutine")
    For A = 1 To 5
         Console.WriteLine(" ")
    Next
    
    Console.WriteLine("Enter the beginning date (xx/xx/xxxx): ")
    BeginningDate = Console.ReadLine()
    Console.WriteLine("Enter the ending date (xx/xx/xxxx): ")
    BeginningDate = Console.ReadLine()
    
    Console.WriteLine(DateDiff("d", EndingDate, BeginningDate) & msg)
    
     
  2. Rebacca

    Rebacca New Member

    Joined:
    Feb 20, 2023
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    It seems there are a couple of issues with your code. First, you are using the
    Code:
    DateDiff
    function before initializing the
    Code:
    BeginningDate
    and
    Code:
    EndingDate
    variables. Second, you are reading the input into the `BeginningDate` variable twice instead of initializing `EndingDate`.

    Here's the corrected version of your code in VB.NET:

    Code:
    vbnet
    Module Module1
        Sub Main()
            Dim BeginningDate As Date
            Dim EndingDate As Date
    
            Console.WriteLine("                          Days Between Dates Subroutine")
            For A = 1 To 5
                Console.WriteLine(" ")
            Next
    
            Console.WriteLine("Enter the beginning date (MM/dd/yyyy): ")
            BeginningDate = Date.Parse(Console.ReadLine()) ' Read the beginning date
    
            Console.WriteLine("Enter the ending date (MM/dd/yyyy): ")
            EndingDate = Date.Parse(Console.ReadLine()) ' Read the ending date
    
            Dim DaysBetweenDates = DateDiff(DateInterval.Day, BeginningDate, EndingDate)
            Dim msg = String.Format(" days between dates.", DaysBetweenDates)
    
            Console.WriteLine(DateDiff("d", BeginningDate, EndingDate) & msg)
        End Sub
    End Module

    Changes made:
    1. Initialized `BeginningDate` and `EndingDate` variables before using them in the `DateDiff` function.
    2. Corrected the `DateDiff` function by swapping the `BeginningDate` and `EndingDate` parameters.
    3. Removed unnecessary code lines.

    With these changes, your program should now correctly calculate the number of days between the provided dates. Make sure to input the dates in the format "MM/dd/yyyy" for the program to work as expected.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice