How to add N weeks to the current date in VB.NET

1 Answer

0 votes
Imports System

Module DateModifier

    ''' <summary>
    ''' Adds a specified number of weeks to the current system date.
    ''' </summary>
    ''' <param name="weeks">The number of weeks to add (supports negative numbers to subtract).</param>
    ''' <returns>Date The resulting future date.</returns>
    Public Function AddWeeksToCurrentDate(weeks As Integer) As Date
        ' 1. Get the current local date and time
        Dim currentDate As Date = Date.Now
        
        ' 2. .NET does not have a native AddWeeks method, 
        '    so we multiply weeks by 7 and add them via AddDays.
        Return currentDate.AddDays(weeks * 7)
    End Function

    Sub Main()
        Dim weeksToAdd As Integer = 4
        
        ' Calculate the future date
        Dim futureDate As Date = AddWeeksToCurrentDate(weeksToAdd)

        ' Output formatted cleanly as YYYY-MM-DD using string interpolation
        Console.WriteLine($"Adding {weeksToAdd} weeks to today's date gives: {futureDate:yyyy-MM-dd}")
    End Sub

End Module
 
 
' run:
'
' Adding 4 weeks to today's date gives: 2026-12-05
'

 



answered 1 day ago by avibootz
...