How to add N weeks to the current date in C#

1 Answer

0 votes
using System;

public class DateUtility
{
    /// <summary>
    /// Adds a specified number of weeks to the current system date and time.
    /// </summary>
    /// <param name="weeks">The number of weeks to add (supports negative values to subtract).</param>
    /// <returns>DateTime The resulting future date and time.</returns>
    public static DateTime AddWeeksToCurrentDate(int weeks)
    {
        // 1. Get the current local date and time
        DateTime currentDate = DateTime.Now;

        // 2. Since DateTime lacks an explicit AddWeeks method, 
        //    multiply the weeks by 7 and add them via AddDays.
        return currentDate.AddDays(weeks * 7);
    }

    public static void Main()
    {
        int weeksToAdd = 4;

        // Calculate the future date
        DateTime futureDate = 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}");
    }
}


/*
run:

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

*/

 



answered 1 day ago by avibootz
...