How to add N weeks to a given date in C#

1 Answer

0 votes
using System;

class DateAdditionExample
{
    /// <summary>
    /// Adds a specified number of weeks to a given DateTime.
    /// </summary>
    /// <param name="baseDate">The original DateTime value.</param>
    /// <param name="weeks">The number of weeks to add (can also be negative to subtract).</param>
    /// <returns>A new DateTime with the added weeks.</returns>
    public static DateTime AddWeeksToDate(DateTime baseDate, int weeks)
    {
        // DateTime does not have a direct AddWeeks method, 
        // so we multiply weeks by 7 and use AddDays.
        return baseDate.AddDays(weeks * 7);
    }

    static void Main()
    {
        // Initialize a sample date: November 8, 2026
        DateTime targetDate = new DateTime(2026, 11, 8);
        int weeksToAdd = 4;

        // Perform the addition
        DateTime resultDate = AddWeeksToDate(targetDate, weeksToAdd);

        // Display the formatted results using interpolated strings
        Console.WriteLine($"Original date:        {targetDate:yyyy-MM-dd}");
        Console.WriteLine($"After adding {weeksToAdd} weeks: {resultDate:yyyy-MM-dd}");
    }
}


/*
run:

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

*/

 



answered 19 hours ago by avibootz
...