How to calculate the number of days until Christmas from today in VB.NET

1 Answer

0 votes
Imports System

Module Program

    '
    ' ---------------------------------------------------------
    ' Function: DaysUntilChristmas
    ' Purpose : Calculate how many days remain until Christmas.
    ' ---------------------------------------------------------
    '
    Function DaysUntilChristmas() As Integer

        ' Get today's date from the system
        Dim today As DateTime = DateTime.Today

        ' Extract the current year
        Dim year As Integer = today.Year

        ' Build a date for Christmas of the current year
        Dim christmas As New DateTime(year, 12, 25)

        ' If Christmas already passed this year, calculate for next year
        If today > christmas Then
            christmas = New DateTime(year + 1, 12, 25)
        End If

        ' Calculate difference in days between today and Christmas
        Dim days As Integer = CInt((christmas - today).TotalDays)

        Return days
    End Function

    Sub Main()
        Dim days As Integer = DaysUntilChristmas()
		
        Console.WriteLine("Days until Christmas: " & days)
    End Sub

End Module



'
' run:
'
' Days until Christmas: 211
'

 



answered 1 hour ago by avibootz
...