How to add N weeks to a given date in VB.NET

1 Answer

0 votes
Imports System

Module DateAdditionExample

    ''' <summary>
    ''' Adds a specified number of weeks to a given Date.
    ''' </summary>
    ''' <param name="baseDate">The original Date value.</param>
    ''' <param name="weeks">The number of weeks to add (can also be negative to subtract).</param>
    ''' <returns>A new Date with the added weeks.</returns>
    Public Function AddWeeksToDate(baseDate As Date, weeks As Integer) As Date
        ' DateTime does not have a direct AddWeeks method, 
        ' so we multiply weeks by 7 and use AddDays.
        Return baseDate.AddDays(weeks * 7)
    End Function

    Sub Main()
        ' Initialize a sample date: November 8, 2026
        Dim targetDate As Date = New Date(2026, 11, 8)
        Dim weeksToAdd As Integer = 4

        ' Perform the addition
        Dim resultDate As Date = AddWeeksToDate(targetDate, weeksToAdd)

        ' Display the formatted results
        Console.WriteLine("Original date:        {0:yyyy-MM-dd}", targetDate)
        Console.WriteLine("After adding {0} weeks: {1:yyyy-MM-dd}", weeksToAdd, resultDate)
    End Sub

End Module

 
' run:
'
' Original date:        2026-11-08
' After adding 4 weeks: 2026-12-06
'

 



answered 19 hours ago by avibootz
...