Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to compare two dates in VB.NET

1 Answer

0 votes
Imports System
Imports System.Globalization

Module CompareDatesVB

    '
    ' Compare two dates in VB.NET
    ' ---------------------------
    ' This program demonstrates how to compare two dates using VB.NET's built‑in
    ' DateTime structure. It uses a modular design, clear comments, and a full test suite.
    '
    ' Concepts:
    '   - Parsing dates safely (YYYY‑MM‑DD)
    '   - Comparing DateTime objects
    '   - Handling invalid formats
    '   - Edge‑case testing
    '
    ' Architecture notes:
    '   - A dedicated function handles parsing.
    '   - Another function performs comparison.
    '   - Main runs multiple predefined test cases.
    '   - No external dependencies; only standard library.
    '
    ' Performance notes:
    '   - DateTime comparisons are O(1).
    '   - Parsing is fast and predictable.
    '   - Memory usage is minimal.
    '
    ' Pitfalls:
    '   - Invalid date strings must be handled.
    '   - Comparing raw strings is unsafe; always convert to DateTime.
    '   - DateTime is timezone‑aware; here we treat dates as date‑only values.
    '

    ' Safely parse a date string in the format YYYY-MM-DD.
    ' Returns Nothing if the date is invalid.
    Function ParseDate(s As String) As DateTime?
        Try
            Return DateTime.ParseExact(
                s,
                "yyyy-MM-dd",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None
            )
        Catch
            Return Nothing
        End Try
    End Function

    ' Compare two DateTime objects.
    ' Returns: "earlier", "later", or "equal".
    Function CompareDates(a As DateTime, b As DateTime) As String
        If a < b Then Return "earlier"
        If a > b Then Return "later"
        Return "equal"
    End Function

    ' Run a single test case:
    '   - Parse both dates
    '   - Handle invalid input
    '   - Compare if valid
    Sub RunTestCase(d1 As String, d2 As String)
        Dim a = ParseDate(d1)
        Dim b = ParseDate(d2)

        If a Is Nothing OrElse b Is Nothing Then
            Console.WriteLine($"Compare '{d1}' vs '{d2}' → invalid date format")
            Exit Sub
        End If

        Console.WriteLine($"Compare '{d1}' vs '{d2}' → {CompareDates(a.Value, b.Value)}")
    End Sub

    ' Main test suite:
    '   - Multiple test cases
    '   - Includes edge cases
    '   - Prints results cleanly
    Sub Main()
        Console.WriteLine("Date comparison tests:")
        Console.WriteLine()

        Dim tests(,) As String = {
            {"2024-01-01", "2024-01-02"},   ' earlier
            {"2024-01-02", "2024-01-01"},   ' later
            {"2024-01-01", "2024-01-01"},   ' equal
            {"1999-12-31", "2000-01-01"},   ' millennium boundary
            {"2024-02-29", "2024-03-01"},   ' leap year
            {"2024-02-29", "2023-02-28"},   ' leap vs non-leap
            {"2024-13-01", "2024-01-01"},   ' invalid month
            {"2024-00-10", "2024-01-01"},   ' invalid month
            {"2024-01-32", "2024-01-01"},   ' invalid day
            {"abcd-ef-gh", "2024-01-01"},   ' invalid format
            {"2024-01-01", "abcd-ef-gh"}    ' invalid format
        }

        For i As Integer = 0 To tests.GetLength(0) - 1
            RunTestCase(tests(i, 0), tests(i, 1))
        Next
    End Sub

End Module

		
'
' run:
'
' Date comparison tests:
'
' Compare '2024-01-01' vs '2024-01-02' → earlier
' Compare '2024-01-02' vs '2024-01-01' → later
' Compare '2024-01-01' vs '2024-01-01' → equal
' Compare '1999-12-31' vs '2000-01-01' → earlier
' Compare '2024-02-29' vs '2024-03-01' → earlier
' Compare '2024-02-29' vs '2023-02-28' → later
' Compare '2024-13-01' vs '2024-01-01' → invalid date format
' Compare '2024-00-10' vs '2024-01-01' → invalid date format
' Compare '2024-01-32' vs '2024-01-01' → invalid date format
' Compare 'abcd-ef-gh' vs '2024-01-01' → invalid date format
' Compare '2024-01-01' vs 'abcd-ef-gh' → invalid date format
' 
'

 



answered 1 day ago by avibootz
...