How to compute the day of the week for January 1st of any given year in VB.NET

1 Answer

0 votes
Imports System

Module Jan1WeekdayProgram

    '
    '   This program determines the day of the week for January 1st of a given year.
    '
    '   Approach:
    '   ---------
    '   VB.NET provides built‑in date/time handling through the Date structure:
    '       - New Date(year, month, day) : constructs a calendar date
    '       - Date.DayOfWeek             : returns a DayOfWeek enum value
    '
    '   DayOfWeek values:
    '       Sunday, Monday, Tuesday, Wednesday,
    '       Thursday, Friday, Saturday
    '
    '   This avoids manual calendar arithmetic and uses efficient built‑in routines.
    '

    ' Convert DayOfWeek enum to a readable string
    Function WeekdayName(dow As DayOfWeek) As String
        ' Convert enum name to "Title Case"
        Dim s As String = dow.ToString()
		
        Return Char.ToUpper(s(0)) & s.Substring(1).ToLower()
    End Function

    ' Compute weekday of January 1st for a given year
    Function Jan1Weekday(year As Integer) As String
        Dim dateValue As New Date(year, 1, 1)   ' January 1st of given year
        Dim dow As DayOfWeek = dateValue.DayOfWeek
		
        Return WeekdayName(dow)
    End Function

    Sub Main()
        Dim year As Integer = 2026

        Dim resultStr As String = Jan1Weekday(year)
        Console.WriteLine("January 1st, " & year & " falls on a " & resultStr & ".")
    End Sub

End Module



' run:
' 
' January 1st, 2026 falls on a Thursday.
'

 



answered 11 hours ago by avibootz

Related questions

...